StructArray into KeyedArray

jar1
Sat, 30 Nov 2024 22:06:57 +0000
General Usage

How do I turn a StructArray into a KeyedArray?

┌───────┬───────┬───────────┬──────────┬──────────┐
│     a │     b │         x │        y │        z │
│ Int64 │ Int64 │   Float64 │  Float64 │  Float64 │
├───────┼───────┼───────────┼──────────┼──────────┤
│     1 │     3 │ 0.0271853 │ 0.426652 │ 0.908247 │
│     2 │     3 │  0.331786 │ 0.470659 │  0.80698 │
│     1 │     4 │  0.636678 │ 0.350289 │ 0.708534 │
│     2 │     4 │ 0.0822821 │ 0.263246 │ 0.981152 │
└───────┴───────┴───────────┴──────────┴──────────┘

using StructArrays, AxisKeys, DataManipulation
let k = 4
    d = (;
        a = 1:k,
        b = [1,1,2,2],
        x = rand(k),
        y = rand(k),
        z = rand(k),
    ) |> StructArray

    @p begin
        d
        group(_.b)
        map(pairs(__)) do (b, __)
            group(_.a)
            map() do __
                [__.x __.y __.z]
                KeyedArray(; row=[1], col=[:x, :y, :z])
            end
            KeyedArray(collect(__); a=collect(keys(__)))
            stack
            dropdims(;dims=1)
        end
        KeyedArray(collect(__); b=collect(keys(__)))
        stack
    end
end
ERROR: DimensionMismatch: stack expects uniform axiskeys for all arrays

7 posts - 3 participants