I'm new to the StructArray.jl package. Given that I have
s = StructArray((a=[1,2], b =[1,1]))
c = [5,5]
How can I then create a new StructArray with a column c
, i.e. StructArray((a=[1,2], b =[1,1], c=[5,5]))
?
In other words, how can I "add" a column to a StructArray?
I feel like this should work.
julia> @set StructArray((;a=[1,2], b=[10,20])).c = [100,200]
ERROR: ArgumentError: Failed to assign properties (:c,) to object with properties (:a, :b).
:/
I can "brute force" by looping over the properties in the StructArray ,turning them into a namedtuple and then transforming the whole thing into a StructArray again.
But this sounds inefficient... ?
@jar just use @insert
instead of @set
:)
julia> using Accessors
julia> @insert s.c = [5, 5]
2-element StructArray(::Vector{Int64}, ::Vector{Int64}, ::Vector{Int64}) with eltype @NamedTuple{a::Int64, b::Int64, c::Int64}:
(a = 1, b = 1, c = 5)
(a = 2, b = 1, c = 5)
@Davi Sales Barreira under the hood, @insert
does exactly as you say: extracts columns as a namedtuple, adds another element there, and construct a new structarray. This is efficient, basically free!
Davi Sales Barreira has marked this topic as resolved.
Last updated: Nov 06 2024 at 04:40 UTC