NamedArray specifying only dimension names

In NamedArrays, is it possible to specify ("Rows", "Cols") but leaving the row and column numbers as 1,2,…?

julia> NamedArray([1 3; 2 4], ( OrderedDict("A"=>1, "B"=>2), OrderedDict("C"=>1, "D"=>2) ), ("Rows", "Cols") )
2×2 Named Matrix{Int64}
Rows ╲ Cols │ C  D
────────────┼─────
A           │ 1  3
B           │ 2  4

julia> NamedArray([1 3; 2 4], ("Rows", "Cols") )
ERROR: MethodError: no method matching NamedArray(::Matrix{Int64}, ::Tuple{String, String})

Don’t think it’s possible, it definitely wasn’t when I tried this package some time ago.

A more flexible alternative is GitHub - invenia/NamedDims.jl: For working with dimensions of arrays by name + GitHub - mcabbott/AxisKeys.jl: 🎹. The former provides named dimensions, which is exactly what you want. The latter gives custom names for rows/columns/…, and these packages are designed to work either by themselves or together.
Your example would look like NamedDimsArray([1 3; 2 4], (:Rows, :Cols)).

I tested the “NamedArrays” in this way

using NamedArrays, Plots
Plots.default(legend=false, grid=false, size=(700,300))

myArray=NamedArray(rand(-10:40, 31,12))
setnames!(myArray, ["Jan", "Feb", "Mar", "Apr", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"], 2)
show(myArray)

month="Dec"
plot(myArray[1:end,get(myArray.dicts[2],month,"Name of month is wrong")], title=month)

Your way to query the array, IMO seems more sensefull with “MetaArrays”.

1 Like