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.889194 0.356028 0.550553 0.986804 0.495366 0.433724
2 0.685603 0.295265 0.143856 0.721889 0.778193 0.197531
3 0.0987646 0.660911 0.496545 0.571628 0.652397 0.207916
⋮ ⋱ ⋮
7 0.650311 0.240351 0.0933704 0.773992 0.318512 0.365215
8 0.908068 0.390515 0.782823 0.91991 0.301929 0.113556
9 0.73427 0.71403 0.994093 0.537065 0.66882 0.579501
10 0.122976 0.731753 0.448132 … 0.332876 0.0648408 0.993835
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.889194
2 │ 2 1 a 0.685603
3 │ 3 1 a 0.0987646
4 │ 4 1 a 0.191188
5 │ 5 1 a 0.122571
6 │ 6 1 a 0.418412
7 │ 7 1 a 0.650311
8 │ 8 1 a 0.908068
⋮ │ ⋮ ⋮ ⋮ ⋮
2594 │ 4 10 z 0.210417
2595 │ 5 10 z 0.849817
2596 │ 6 10 z 0.261216
2597 │ 7 10 z 0.0459272
2598 │ 8 10 z 0.434794
2599 │ 9 10 z 0.768234
2600 │ 10 10 z 0.322995
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.19219227965820063,0.48952511607945026"
"2,1,a,0.6072910004472037,0.7938317326707394"
"3,1,a,0.9219584479428687,0.0019198597596568057"
"4,1,a,0.449490631413745,0.8612776980335002"
"5,1,a,0.5811306546643178,0.20758428874582302"
"6,1,a,0.1948490023468078,0.023646798570656102"
"7,1,a,0.3640965349509072,0.4000537338886899"
"8,1,a,0.022593018091968675,0.24835810922448598"
"9,1,a,0.8286044504480337,0.50867932921777"
⋮
"2,10,z,0.8872944242976297,0.6858008906364156"
"3,10,z,0.012096736709184541,0.7959265671836858"
"4,10,z,0.26634216134156385,0.3777991041100621"
"5,10,z,0.6204733381493495,0.2276004407628871"
"6,10,z,0.9853765098437419,0.1132529224292641"
"7,10,z,0.25236585444042137,0.25073570045665916"
"8,10,z,0.9656269833042522,0.40747087988600206"
"9,10,z,0.42464889228012526,0.8206679793700835"
"10,10,z,0.4077154920607551,0.9718806401138506"