Tables and DataFrames
Tables.jl provides an ecosystem-wide interface to tabular data in julia, giving interop 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.dim2key(dimension)
).
Looping of dimensions and stack layers is done lazily, and does not allocate unless collected.
Example
using DimensionalData, Dates, 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.98923 0.494425 0.0485103 0.591008 0.695832 0.0954375
2 0.132046 0.172035 0.988767 0.425177 0.521827 0.681577
3 0.811807 0.898238 0.835698 0.703425 0.0857436 0.137483
4 0.986466 0.962102 0.536363 0.0118509 0.5543 0.0438266
⋮ ⋱ ⋮
7 0.132927 0.62582 0.270968 0.701969 0.671722 0.105779
8 0.659444 0.696955 0.955354 0.215741 0.112198 0.734391
9 0.541292 0.756739 0.243488 0.21418 0.465607 0.231252
10 0.0443724 0.650058 0.396104 … 0.770442 0.923159 0.475621
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.98923
2 │ 2 1 a 0.132046
3 │ 3 1 a 0.811807
4 │ 4 1 a 0.986466
5 │ 5 1 a 0.822923
6 │ 6 1 a 0.17712
7 │ 7 1 a 0.132927
8 │ 8 1 a 0.659444
⋮ │ ⋮ ⋮ ⋮ ⋮
2594 │ 4 10 z 0.451226
2595 │ 5 10 z 0.320397
2596 │ 6 10 z 0.828402
2597 │ 7 10 z 0.942326
2598 │ 8 10 z 0.259623
2599 │ 9 10 z 0.720341
2600 │ 10 10 z 0.69682
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.
julia> using CSV
julia> CSV.write("dimstack.csv", st)
"dimstack.csv"
julia> readlines("dimstack.csv")
2601-element Vector{String}:
"X,Y,category,data1,data2"
"1,1,a,0.8925456476827067,0.6754285497685953"
"2,1,a,0.08453433283655587,0.9364272097621991"
"3,1,a,0.009117443658263502,0.34716916927339125"
"4,1,a,0.3429860397050317,0.029231223586021038"
"5,1,a,0.661849081229783,0.17185101336631825"
"6,1,a,0.39187220290678226,0.4714168586103352"
"7,1,a,0.029191993104933922,0.947654384327241"
"8,1,a,0.7089801287974157,0.1526833034766708"
"9,1,a,0.3752691165022767,0.17700228416642227"
⋮
"2,10,z,0.6393587977015998,0.04353900096422991"
"3,10,z,0.6746507486667644,0.5261597289524632"
"4,10,z,0.01090262641691575,0.20515070331860819"
"5,10,z,0.29450159501715556,0.8854894267628785"
"6,10,z,0.7133255046656136,0.7042215164956682"
"7,10,z,0.9272611825896064,0.17036100971286572"
"8,10,z,0.9388715092143414,0.2797753964779216"
"9,10,z,0.8375022559753681,0.25497854880545423"
"10,10,z,0.8236655973126321,0.06645252184044714"