AxisKeys.jl
AxisKeys.jl copied to clipboard
Support diff
e.g.
x = KeyedArray(rand(10, 20), x=1:10, y=1:20)
diff(x; dims=:x)
Currently, diff(x; dims=1) errors because diff changes the size of the array. There is some ambiguity about the correct way to handle keys in the result. In some strict sense, the keys should be something like "Δ(1, 2)". But I think the most natural/useful thing would probably be to just use the lower label (so, drop 10).
I use this:
function Base.diff(K::KeyedArray; dims, removefirst::Bool=true)
range = removefirst ? (2:size(K, dims)) : (1:size(K,dims)-1)
out = similar(selectdim(K, dims, range) )
out[:] = Base.diff(parent(parent(K)); dims=NamedDims.dim(parent(K),dims))
return out
end