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.647752    0.0743074  0.780061      0.716192   0.209985   0.815375
  2    0.225171    0.777718   0.910775      0.868543   0.969675   0.584832
  3    0.308035    0.956535   0.0500622     0.552811   0.930901   0.5924
  4    0.754535    0.500805   0.0929471     0.34002    0.430145   0.540943
  ⋮                                      ⋱                        ⋮
  7    0.721265    0.737964   0.153455      0.394934   0.69321    0.207701
  8    0.935965    0.455218   0.972894      0.221934   0.807245   0.152262
  9    0.817648    0.0296907  0.926061      0.674892   0.301445   0.955318
 10    0.00131062  0.962695   0.50865    …  0.0024964  0.142083   0.762599

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.647752
    2 │     2      1  a         0.225171
    3 │     3      1  a         0.308035
    4 │     4      1  a         0.754535
    5 │     5      1  a         0.587235
    6 │     6      1  a         0.418407
    7 │     7      1  a         0.721265
    8 │     8      1  a         0.935965
  ⋮   │   ⋮      ⋮       ⋮           ⋮
 2594 │     4     10  z         0.427285
 2595 │     5     10  z         0.076728
 2596 │     6     10  z         0.131343
 2597 │     7     10  z         0.20917
 2598 │     8     10  z         0.628095
 2599 │     9     10  z         0.388557
 2600 │    10     10  z         0.308934
                           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.6475578700195562,0.392503027515828"
 "2,1,a,0.2715500029218121,0.3925971153380383"
 "3,1,a,0.20927210163851506,0.9232885800886012"
 "4,1,a,0.9092944486539056,0.06459332839348286"
 "5,1,a,0.4908273081677056,0.09486401764194341"
 "6,1,a,0.828912505629795,0.6689453145026014"
 "7,1,a,0.726701850721938,0.09342902387820096"
 "8,1,a,0.17811137582716385,0.5009876431478334"
 "9,1,a,0.15344429376806057,0.618913151483759"

 "2,10,z,0.04228222018098238,0.16407841334744733"
 "3,10,z,0.841568589903311,0.907265146834413"
 "4,10,z,0.12852747154186728,0.5666940599881654"
 "5,10,z,0.21458389386349508,0.7761736819284226"
 "6,10,z,0.237191561947401,0.6813479976784406"
 "7,10,z,0.5463288708383782,0.5649205528517407"
 "8,10,z,0.7276491086642856,0.10416433197078767"
 "9,10,z,0.7727811701635752,0.7435610591768332"
 "10,10,z,0.04124972172377106,0.9264513771359043"