Tables and DataFrames
Tables.jl provides an ecosystem-wide interface to tabular data in Julia, ensuring 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
of the array or stack layer, and the result of DD.name(dimension)
for Dimension
columns.
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.599241 0.518938 0.341133 0.486559 0.162516 0.934189
2 0.192192 0.336376 0.636476 0.840593 0.687569 0.294486
3 0.607291 0.963657 0.353968 0.120955 0.434286 0.887294
4 0.921958 0.128827 0.517175 0.101305 0.743407 0.0120967
⋮ ⋱ ⋮
7 0.194849 0.975511 0.612828 0.888721 0.890574 0.436622
8 0.364097 0.163103 0.142055 0.049689 0.259847 0.570725
9 0.394448 0.755939 0.54624 0.156388 0.210664 0.966517
10 0.828604 0.359421 0.51621 … 0.828161 0.107233 0.74172
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.599241
2 │ 2 1 a 0.192192
3 │ 3 1 a 0.607291
4 │ 4 1 a 0.921958
5 │ 5 1 a 0.449491
6 │ 6 1 a 0.581131
7 │ 7 1 a 0.194849
8 │ 8 1 a 0.364097
⋮ │ ⋮ ⋮ ⋮ ⋮
2594 │ 4 10 z 0.852872
2595 │ 5 10 z 0.0958843
2596 │ 6 10 z 0.315302
2597 │ 7 10 z 0.236866
2598 │ 8 10 z 0.894053
2599 │ 9 10 z 0.350024
2600 │ 10 10 z 0.417756
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.55560637324799,0.845200516911609"
"2,1,a,0.10276733254788795,0.9104238640380062"
"3,1,a,0.22237128922242078,0.8268020755919178"
"4,1,a,0.5501481631111826,0.9447511416331498"
"5,1,a,0.09300753748828394,0.15945803739833375"
"6,1,a,0.48952511607945026,0.6146564273146751"
"7,1,a,0.7938317326707394,0.9770663775826343"
"8,1,a,0.0019198597596568057,0.798655984630017"
"9,1,a,0.44833963865079907,0.40268027828179853"
⋮
"2,10,z,0.9675326879984427,0.41940525122635797"
"3,10,z,0.5099922507050859,0.07986058669268159"
"4,10,z,0.3053673139967894,0.4496996354823414"
"5,10,z,0.8146121812750928,0.9452913850518949"
"6,10,z,0.38167574879167476,0.24524306337289326"
"7,10,z,0.17977958441149666,0.1985699519321249"
"8,10,z,0.7044663405368152,0.694278906020718"
"9,10,z,0.5697400488168892,0.20636222545147498"
"10,10,z,0.8560905731682101,0.8428656510212863"