Skip to content

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

julia
using DimensionalData, Dates, DataFrames

Define some dimensions:

julia
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
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         38          9         10
  1    0.470268  0.154606  0.600183     0.826882   0.532517   0.735423
  2    0.879138  0.478282  0.99354      0.627527   0.537662   0.433843
  3    0.592516  0.116622  0.454178     0.512326   0.330347   0.285959
  4    0.267772  0.983627  0.654952     0.72398    0.535475   0.641824
  ⋮                                  ⋱                        ⋮
  7    0.611447  0.546637  0.121093     0.0911216  0.135015   0.959625
  8    0.553796  0.123322  0.788404     0.490502   0.517555   0.591938
  9    0.801063  0.562606  0.474802     0.907274   0.68735    0.339445
 10    0.861614  0.57546   0.953671  …  0.558264   0.209093   0.0620265

Converting to DataFrame

Arrays will have columns for each dimension, and only one data column

julia
julia> DataFrame(A)
2600×4 DataFrame
  Row │ X      Y      category  data
 Int64  Int64  Char      Float64
──────┼──────────────────────────────────
    1 │     1      1  a         0.470268
    2 │     2      1  a         0.879138
    3 │     3      1  a         0.592516
    4 │     4      1  a         0.267772
    5 │     5      1  a         0.911425
    6 │     6      1  a         0.951677
    7 │     7      1  a         0.611447
    8 │     8      1  a         0.553796
  ⋮   │   ⋮      ⋮       ⋮         ⋮
 2594 │     4     10  z         0.538464
 2595 │     5     10  z         0.881027
 2596 │     6     10  z         0.311116
 2597 │     7     10  z         0.100898
 2598 │     8     10  z         0.802703
 2599 │     9     10  z         0.977354
 2600 │    10     10  z         0.959202
                        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
julia> using CSV

julia> CSV.write("dimstack.csv", st)
"dimstack.csv"
julia
julia> readlines("dimstack.csv")
2601-element Vector{String}:
 "X,Y,category,data1,data2"
 "1,1,a,0.4605058291239784,0.9423165304676713"
 "2,1,a,0.24856248801349368,0.3695664029458353"
 "3,1,a,0.025456898312783638,0.518507038649619"
 "4,1,a,0.6088872962119832,0.8144727203724785"
 "5,1,a,0.1422157639054069,0.6411458485384581"
 "6,1,a,0.5867860840487937,0.8263803772476035"
 "7,1,a,0.7870018243062714,0.46133879904026354"
 "8,1,a,0.9243964854537459,0.6169768514041832"
 "9,1,a,0.689460731772713,0.3774337811176909"

 "2,10,z,0.15772699334677898,0.7170162183697867"
 "3,10,z,0.17499288351764264,0.24956056468998145"
 "4,10,z,0.6415224975429407,0.2184431033158818"
 "5,10,z,0.23549708581907058,0.19727538823514623"
 "6,10,z,0.028141971590686987,0.2788237057367504"
 "7,10,z,0.4160342546940823,0.3075490176768796"
 "8,10,z,0.6298192089058043,0.5345337510797605"
 "9,10,z,0.7785013234345299,0.8646962326245329"
 "10,10,z,0.5010116054381656,0.2427574257935644"