rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
281 stars 42 forks source link

Tables.columntable fails for 0-dimensional DimArray #424

Closed sethaxen closed 1 year ago

sethaxen commented 1 year ago
julia> using DataFrames, Tables

julia> x = DimArray(fill(3), (); name=:x);

julia> y = DimArray(randn(2, 3), (Dim{:a}(1:2), Dim{:b}(1:3)));

julia> Tables.columntable(y)  # fine for >0-dim array
(a = [1, 2, 1, 2, 1, 2], b = [1, 1, 2, 2, 3, 3], var"" = [-1.5090420829521751, 1.78624724297272, -0.629637266507813, -1.341032743849202, -1.1542533262114874, 0.2629446089245767])

julia> Tables.columntable(x)  # fails for 0-dim array
ERROR: MethodError: no method matching DimensionalData.DimArrayColumn(::DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, Symbol, DimensionalData.Dimensions.LookupArrays.NoMetadata}, ::Tuple{})
Closest candidates are:
  DimensionalData.DimArrayColumn(::A, ::DS, ::DL, ::L) where {T, A<:(AbstractDimArray{T}), DS, DL, L} at ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:74
  DimensionalData.DimArrayColumn(::AbstractDimArray{T}, ::Tuple{DimensionalData.Dimensions.Dimension, Vararg{DimensionalData.Dimensions.Dimension}}) where T at ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:79
Stacktrace:
  [1] (::DimensionalData.var"#301#303"{Tuple{}})(A::DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, Symbol, DimensionalData.Dimensions.LookupArrays.NoMetadata})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:149
  [2] map
    @ ./tuple.jl:221 [inlined]
  [3] map(::Function, ::NamedTuple{(Symbol(""),), Tuple{DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, Symbol, DimensionalData.Dimensions.LookupArrays.NoMetadata}}})
    @ Base ./namedtuple.jl:219
  [4] map(f::Function, s::DimStack{NamedTuple{(Symbol(""),), Tuple{Array{Int64, 0}}}, Tuple{}, Tuple{}, NamedTuple{(Symbol(""),), Tuple{Tuple{}}}, DimensionalData.Dimensions.LookupArrays.NoMetadata, NamedTuple{(Symbol(""),), Tuple{DimensionalData.Dimensions.LookupArrays.NoMetadata}}})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/stack/methods.jl:50
  [5] DimTable(s::DimStack{NamedTuple{(Symbol(""),), Tuple{Array{Int64, 0}}}, Tuple{}, Tuple{}, NamedTuple{(Symbol(""),), Tuple{Tuple{}}}, DimensionalData.Dimensions.LookupArrays.NoMetadata, NamedTuple{(Symbol(""),), Tuple{DimensionalData.Dimensions.LookupArrays.NoMetadata}}})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:149
  [6] DimTable(As::Tuple{DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, DimensionalData.NoName, DimensionalData.Dimensions.LookupArrays.NoMetadata}})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:144
  [7] DimTable(::DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, DimensionalData.NoName, DimensionalData.Dimensions.LookupArrays.NoMetadata})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:142
  [8] columns(x::DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, DimensionalData.NoName, DimensionalData.Dimensions.LookupArrays.NoMetadata})
    @ DimensionalData ~/.julia/packages/DimensionalData/K9D4P/src/tables.jl:7
  [9] columntable(itr::DimArray{Int64, 0, Tuple{}, Tuple{}, Array{Int64, 0}, DimensionalData.NoName, DimensionalData.Dimensions.LookupArrays.NoMetadata})
    @ Tables ~/.julia/packages/Tables/T7rHm/src/namedtuples.jl:183
 [10] top-level scope
    @ REPL[28]:1

julia> ds = DimStack((; x, y));

julia> Tables.columntable(ds)  # but having both in a single DimStack is fine 
(a = [1, 2, 1, 2, 1, 2], b = [1, 1, 2, 2, 3, 3], x = [3, 3, 3, 3, 3, 3], y = [-1.5090420829521751, 1.78624724297272, -0.629637266507813, -1.341032743849202, -1.1542533262114874, 0.2629446089245767])

I would expect for Tables.columntable(x) to return (; x=[3]).