Tables and DataFrames
Tables.jl provides an ecosystem-wide interface to tabular data in Julia, giving interoperability with DataFrames.jl, CSV.jl and hundreds of other packages that implement the standard.
DimensionalData.jl implements the Tables.jl interface for AbstractDimArray
and AbstractDimStack
. DimStack
layers are unrolled so they are all the same size, and dimensions loop to match the length of the largest layer.
Columns are given the name
or the array or the stack layer key. Dimension
columns use the Symbol
version (the result of DD.name(dimension)
).
Looping of dimensions and stack layers is done lazily, and does not allocate unless collected.
Example
using DimensionalData
using Dates
using DataFrames
Define some dimensions:
julia> x, y, c = X(1:10), Y(1:10), Dim{:category}('a':'z')
↓ X 1:10,
→ Y 1:10,
↗ category 'a':1:'z'
julia> A = rand(x, y, c; name=:data)
╭───────────────────────────────────╮
│ 10×10×26 DimArray{Float64,3} data │
├───────────────────────────────────┴───────────────────── dims ┐
↓ X Sampled{Int64} 1:10 ForwardOrdered Regular Points,
→ Y Sampled{Int64} 1:10 ForwardOrdered Regular Points,
↗ category Categorical{Char} 'a':1:'z' ForwardOrdered
└───────────────────────────────────────────────────────────────┘
[:, :, 1]
↓ → 1 2 3 … 8 9 10
1 0.921958 0.128827 0.517175 0.947312 0.743407 0.0120967
2 0.449491 0.176024 0.914911 0.674061 0.291555 0.266342
3 0.581131 0.436605 0.694066 0.0325131 0.645678 0.620473
4 0.194849 0.580091 0.176766 0.888721 0.890574 0.985377
⋮ ⋱ ⋮
7 0.191934 0.359421 0.51621 0.828161 0.516313 0.74172
8 0.450701 0.341133 0.00110588 0.190482 0.245671 0.819605
9 0.336376 0.636476 0.45655 0.16484 0.294486 0.457841
10 0.963657 0.353968 0.199484 … 0.104763 0.887294 0.670607
Converting to DataFrame
Arrays will have columns for each dimension, and only one data column
julia> DataFrame(A)
2600×4 DataFrame
Row │ X Y category data
│ Int64 Int64 Char Float64
──────┼──────────────────────────────────
1 │ 1 1 a 0.921958
2 │ 2 1 a 0.449491
3 │ 3 1 a 0.581131
4 │ 4 1 a 0.194849
5 │ 5 1 a 0.364097
6 │ 6 1 a 0.022593
7 │ 7 1 a 0.191934
8 │ 8 1 a 0.450701
⋮ │ ⋮ ⋮ ⋮ ⋮
2594 │ 4 10 z 0.236866
2595 │ 5 10 z 0.894053
2596 │ 6 10 z 0.350024
2597 │ 7 10 z 0.417756
2598 │ 8 10 z 0.125477
2599 │ 9 10 z 0.599789
2600 │ 10 10 z 0.363373
2585 rows omitted
Converting to CSV
We can also write arrays and stacks directly to CSV.jl, or any other data type supporting the Tables.jl interface.
using CSV
CSV.write("dimstack.csv", st)
readlines("dimstack.csv")
2601-element Vector{String}:
"X,Y,category,data1,data2"
"1,1,a,0.5501481631111826,0.9447511416331498"
"2,1,a,0.09300753748828394,0.15945803739833375"
"3,1,a,0.48952511607945026,0.6146564273146751"
"4,1,a,0.7938317326707394,0.9770663775826343"
"5,1,a,0.0019198597596568057,0.798655984630017"
"6,1,a,0.8612776980335002,0.6730273352488979"
"7,1,a,0.20758428874582302,0.7683418213117802"
"8,1,a,0.023646798570656102,0.012300334322905893"
"9,1,a,0.11925244363082943,0.4278264986513013"
⋮
"2,10,z,0.8146121812750928,0.3434655288098666"
"3,10,z,0.38167574879167476,0.24524306337289326"
"4,10,z,0.8719143923648308,0.1985699519321249"
"5,10,z,0.5040228055200978,0.694278906020718"
"6,10,z,0.35100608350331053,0.20636222545147498"
"7,10,z,0.8560905731682101,0.8428656510212863"
"8,10,z,0.04599641808658339,0.6150940338022266"
"9,10,z,0.005163666306917225,0.768924169642427"
"10,10,z,0.8288995877624121,0.8136963000324107"