Closed DilumAluthge closed 4 years ago
julia> using StructArrays
julia> using DataFrames
julia> struct Foo
x::Int
y::String
end
julia> column_x = Int[1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> column_y = String["a", "b", "c"]
3-element Vector{String}:
"a"
"b"
"c"
julia> columns = (; x = column_x, y = column_y, )
(x = [1, 2, 3], y = ["a", "b", "c"])
julia> s = StructArray{Foo}(columns)
3-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
julia> s[1]
Foo(1, "a")
julia> s[2]
Foo(2, "b")
julia> s[3]
Foo(3, "c")
julia> s.x
3-element Vector{Int64}:
1
2
3
julia> s.y
3-element Vector{String}:
"a"
"b"
"c"
julia> df = DataFrame(s)
3×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ a │
│ 2 │ 2 │ b │
│ 3 │ 3 │ c │
julia> using StructArrays
julia> using DataFrames
julia> struct Foo
x::Int
y::String
end
julia> row_1 = Foo(1, "a")
Foo(1, "a")
julia> row_2 = Foo(2, "b")
Foo(2, "b")
julia> row_3 = Foo(3, "c")
Foo(3, "c")
julia> s = StructArray(Foo[row_1, row_2, row_3])
3-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
julia> s[1]
Foo(1, "a")
julia> s[2]
Foo(2, "b")
julia> s[3]
Foo(3, "c")
julia> s.x
3-element Vector{Int64}:
1
2
3
julia> s.y
3-element Vector{String}:
"a"
"b"
"c"
julia> push!(s, Foo(4, "d"))
4-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
Foo(4, "d")
julia> push!(s, Foo(5, "e"))
5-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
Foo(4, "d")
Foo(5, "e")
julia> push!(s, Foo(6, "f"))
6-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
Foo(4, "d")
Foo(5, "e")
Foo(6, "f")
julia> s[1]
Foo(1, "a")
julia> s[2]
Foo(2, "b")
julia> s[3]
Foo(3, "c")
julia> s[4]
Foo(4, "d")
julia> s[5]
Foo(5, "e")
julia> s[6]
Foo(6, "f")
julia> s.x
6-element Vector{Int64}:
1
2
3
4
5
6
julia> s.y
6-element Vector{String}:
"a"
"b"
"c"
"d"
"e"
"f"
julia> df = DataFrame(s)
6×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ a │
│ 2 │ 2 │ b │
│ 3 │ 3 │ c │
│ 4 │ 4 │ d │
│ 5 │ 5 │ e │
│ 6 │ 6 │ f │
julia> using StructArrays
julia> using DataFrames
julia> using Tables
julia> struct Foo
x::Int
y::String
end
julia> df = DataFrame()
0×0 DataFrame
julia> df.x = Int[1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> df.y = String["a", "b", "c"]
3-element Vector{String}:
"a"
"b"
"c"
julia> df
3×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ a │
│ 2 │ 2 │ b │
│ 3 │ 3 │ c │
julia> s = StructArray{Foo}(Tables.columntable(df))
3-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
julia> s
3-element StructArray(::Vector{Int64}, ::Vector{String}) with eltype Foo:
Foo(1, "a")
Foo(2, "b")
Foo(3, "c")
julia> s[1]
Foo(1, "a")
julia> s[2]
Foo(2, "b")
julia> s[3]
Foo(3, "c")
julia> s.x
3-element Vector{Int64}:
1
2
3
julia> s.y
3-element Vector{String}:
"a"
"b"
"c"