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 DataFramesDefine 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.303608 0.351615 0.409982 0.0863477 0.052552 0.811169
2 0.370339 0.590072 0.0217681 0.19419 0.353535 0.635087
3 0.883659 0.32162 0.158268 0.459552 0.823538 0.69464
⋮ ⋱ ⋮
7 0.589715 0.288365 0.712813 0.767908 0.440554 0.965807
8 0.546973 0.14066 0.866663 0.635436 0.394089 0.615224
9 0.0750317 0.758381 0.883258 0.97869 0.747304 0.398386
10 0.552699 0.799531 0.198566 … 0.169118 0.193369 0.316881Converting 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.303608
2 │ 2 1 a 0.370339
3 │ 3 1 a 0.883659
4 │ 4 1 a 0.218985
5 │ 5 1 a 0.575666
6 │ 6 1 a 0.64193
7 │ 7 1 a 0.589715
8 │ 8 1 a 0.546973
⋮ │ ⋮ ⋮ ⋮ ⋮
2594 │ 4 10 z 0.639834
2595 │ 5 10 z 0.626921
2596 │ 6 10 z 0.286008
2597 │ 7 10 z 0.0525759
2598 │ 8 10 z 0.187586
2599 │ 9 10 z 0.279831
2600 │ 10 10 z 0.504085
2585 rows omittedConverting 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.9607543850887869,0.9219584479428687"
"2,1,a,0.09650864185913732,0.449490631413745"
"3,1,a,0.8891938828683675,0.5811306546643178"
"4,1,a,0.685602900345947,0.1948490023468078"
"5,1,a,0.09876460914116802,0.3640965349509072"
"6,1,a,0.1911884115734762,0.022593018091968675"
"7,1,a,0.12257085597580952,0.1919335094005733"
"8,1,a,0.4184116379426558,0.45070108749632076"
"9,1,a,0.2243512965415927,0.3363756904858566"
⋮
"2,10,z,0.06484080285008487,0.5643897112645162"
"3,10,z,0.433724032114829,0.3153017137323628"
"4,10,z,0.19753059721378563,0.23686630260817787"
"5,10,z,0.2079161455707067,0.8940525195972252"
"6,10,z,0.848784756099022,0.3500239723693068"
"7,10,z,0.1396477104084779,0.41775558170036053"
"8,10,z,0.34064364649128265,0.1254773442564282"
"9,10,z,0.9035243485980183,0.5997889728292368"
"10,10,z,0.803115940382577,0.3633726250682834"