Skip to content

Commit 29c5866

Browse files
authored
Merge branch 'master' into J19
2 parents 748a7b9 + 7929518 commit 29c5866

File tree

9 files changed

+58
-8
lines changed

9 files changed

+58
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AxisKeys"
22
uuid = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
33
license = "MIT"
4-
version = "0.2.9"
4+
version = "0.2.11"
55

66
[deps]
77
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/AxisKeys.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export KeyedArray, axiskeys
66
include("lookup.jl")
77

88
include("names.jl")
9-
export named_axiskeys
9+
export named_axiskeys, rekey
1010
export NamedDimsArray, dimnames, rename # Reexport key NamedDimsArrays things
1111

1212
include("wrap.jl")

src/functions.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,9 @@ function Base.filter!(f, a::KeyedVector)
459459
deleteat!(a, j:lastindex(a))
460460
return a
461461
end
462-
462+
463+
function Base.empty!(v::KeyedVector)
464+
empty!(axiskeys(v, 1))
465+
empty!(v.data)
466+
return v
467+
end

src/names.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,27 @@ function named_axiskeys(A::AbstractArray)
173173
NT = NamedTuple{dimnames(A)}
174174
NT(axiskeys(A))
175175
end
176+
177+
178+
"""
179+
rekey(A, (1:10, [:a, :b]))
180+
rekey(A, 2 => [:a, :b])
181+
rekey(A, :y => [:a, :b])
182+
183+
Rekey a KeyedArray via `Tuple`s or `Pair`s, `dim => newkey`. If `A` also has named
184+
dimensions then you can also pass `dimname => newkey`.
185+
"""
186+
rekey(A::Union{KeyedArray, NdaKa}, k2::Tuple) = KeyedArray(keyless(A), k2)
187+
function rekey(A::Union{KeyedArray, NdaKa}, k2::Pair{<:Integer, <:AbstractVector}...)
188+
dims, vals = first.(k2), last.(k2)
189+
new_key = ntuple(ndims(A)) do d
190+
idx = findfirst(==(d), dims)
191+
idx === nothing ? axiskeys(A, d) : vals[idx]
192+
end
193+
return rekey(A, new_key)
194+
end
195+
196+
function rekey(A::Union{KaNda, NdaKa}, k2::Pair{Symbol, <:AbstractVector}...)
197+
pairs = map(p -> NamedDims.dim(A, p[1]) => p[2], k2)
198+
return rekey(A, pairs...)
199+
end

src/stack.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ function LazyStack.rewrap_like(A, a::NamedTuple)
1414
end
1515

1616
# tuple of arrays
17-
function LazyStack.lazystack(x::Tuple{Vararg{<:KeyedArray}})
17+
function LazyStack.lazystack(x::Tuple{Vararg{KeyedArray}})
1818
KeyedArray(lazystack(map(parent, x)), stack_keys(x))
1919
end
2020

21-
stack_keys(xs::Tuple{Vararg{<:KeyedArray}}) =
21+
stack_keys(xs::Tuple{Vararg{KeyedArray}}) =
2222
(keys_or_axes(first(xs))..., Base.OneTo(length(xs)))
2323

2424
# array of arrays: first strip off outer containers...

src/statsbase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848

4949
for fun in (:std, :var, :cov)
5050
full_name = Symbol("mean_and_$fun")
51-
@eval StatsBase.$full_name(A::KeyedMatrix, wv::Vararg{<:AbstractWeights}; dims=:, corrected::Bool=true, kwargs...) =
51+
@eval StatsBase.$full_name(A::KeyedMatrix, wv::Vararg{AbstractWeights}; dims=:, corrected::Bool=true, kwargs...) =
5252
(
5353
mean(A, wv...; dims=dims, kwargs...),
5454
$fun(A, wv...; dims=dims, corrected=corrected, kwargs...)

src/tables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ end
130130
131131
Populate `A` with the contents of the `value` column in a provided `table`, matching the
132132
[Tables.jl](https://github.com/JuliaData/Tables.jl) API. The `table` must contain columns
133-
corresponding to the keys in `A` and implements `Tables.rows`. If the keys in `A` do not
133+
corresponding to the keys in `A` and implement `Tables.columns`. If the keys in `A` do not
134134
uniquely identify rows in the `table` then an `ArgumentError` is throw. If `force` is true
135135
then the duplicate (non-unique) entries will be overwritten.
136136
"""

test/_basic.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ end
170170

171171
@test_throws Exception N(obs=55) # ideally ArgumentError
172172
@test_throws Exception N(obs='z') # ideally BoundsError
173+
174+
new_obs = [:x, :y, :z]
175+
new_iter = 1:4
176+
177+
# Rekey with Tuple
178+
@test axiskeys(rekey(N, ([:x, :y, :z], 1:4))) == ([:x, :y, :z], 1:4)
179+
# Rekey with dim => axiskey pair
180+
@test axiskeys(rekey(N, 2 => 1:4)) == (['a', 'b', 'c'], 1:4)
181+
# Rekey with dimname => axiskey pair
182+
@test axiskeys(rekey(N, :iter => 1:4)) == (['a', 'b', 'c'], 1:4)
173183
end
174184

175185
@testset "named_axiskeys" begin

test/_functions.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ end
159159

160160
@test axiskeys(filter(isodd, V2),1) isa Vector{Int}
161161
@test dimnames(filter(isodd, V2)) == (:v,)
162-
162+
163163
V4 = wrapdims(rand(1:99, 10), collect(2:11))
164164
V4c = copy(V4)
165165
@test filter!(isodd, V4c) === V4c == filter(isodd, V4)
@@ -299,6 +299,17 @@ end
299299
@test ka == KeyedArray([4, 5, 6.0], a=1:3)
300300
end
301301

302+
@testset "empty!" begin
303+
kv = wrapdims([1, 2, 3, 4, 5, 6.0], a=[:a, :b, :c, :d, :e, :f])
304+
@test kv == empty!(kv)
305+
@test isempty(kv)
306+
307+
# make sure array is not in an invalid state if the emtpy for indices fails
308+
ka = wrapdims([4, 5, 6.0], a=1:3)
309+
@test_throws MethodError empty!(ka)
310+
@test ka == KeyedArray([4, 5, 6.0], a=1:3)
311+
end
312+
302313
@testset "equality" begin
303314

304315
data = parent(parent(M))

0 commit comments

Comments
 (0)