Skip to content

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

julia
using DimensionalData
using Dates
using 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.960754   0.73427   0.71403       0.0450694  0.685225   0.66882
  2    0.0965086  0.122976  0.731753      0.474659   0.391502   0.0648408
  3    0.889194   0.356028  0.550553      0.348197   0.495366   0.433724
  4    0.685603   0.295265  0.143856      0.374729   0.778193   0.197531
  ⋮                                    ⋱                        ⋮
  7    0.122571   0.245564  0.431383      0.258165   0.351907   0.99726
  8    0.418412   0.939201  0.666574      0.0908083  0.802274   0.747231
  9    0.224351   0.240351  0.0933704     0.773992   0.99531    0.365215
 10    0.767136   0.390515  0.782823   …  0.91991    0.605097   0.113556

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.960754
    2 │     2      1  a         0.0965086
    3 │     3      1  a         0.889194
    4 │     4      1  a         0.685603
    5 │     5      1  a         0.0987646
    6 │     6      1  a         0.191188
    7 │     7      1  a         0.122571
    8 │     8      1  a         0.418412
  ⋮   │   ⋮      ⋮       ⋮          ⋮
 2594 │     4     10  z         0.227142
 2595 │     5     10  z         0.635786
 2596 │     6     10  z         0.210417
 2597 │     7     10  z         0.849817
 2598 │     8     10  z         0.261216
 2599 │     9     10  z         0.0459272
 2600 │    10     10  z         0.434794
                         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
CSV.write("dimstack.csv", st)
readlines("dimstack.csv")
2601-element Vector{String}:
 "X,Y,category,data1,data2"
 "1,1,a,0.2674330482715843,0.5501481631111826"
 "2,1,a,0.5992407552660244,0.09300753748828394"
 "3,1,a,0.19219227965820063,0.48952511607945026"
 "4,1,a,0.6072910004472037,0.7938317326707394"
 "5,1,a,0.9219584479428687,0.0019198597596568057"
 "6,1,a,0.449490631413745,0.8612776980335002"
 "7,1,a,0.5811306546643178,0.20758428874582302"
 "8,1,a,0.1948490023468078,0.023646798570656102"
 "9,1,a,0.20144095329862288,0.11925244363082943"

 "2,10,z,0.9341886269251364,0.6005065544080029"
 "3,10,z,0.29448593792551514,0.36851882799081104"
 "4,10,z,0.8872944242976297,0.23350386812772128"
 "5,10,z,0.012096736709184541,0.7959265671836858"
 "6,10,z,0.26634216134156385,0.3777991041100621"
 "7,10,z,0.4858762080349691,0.2276004407628871"
 "8,10,z,0.27135422404853515,0.1132529224292641"
 "9,10,z,0.25236585444042137,0.25073570045665916"
 "10,10,z,0.9656269833042522,0.40747087988600206"