Create missing columns (or rows) in a NamedArray with a default value

Hello,

I would like to transform a Named Array (coming for FreqTable.jl freqtable) from

x = NamedArray([2 0; 0 1; 1 2]);
idx_rows = ["0", "1", "2"]
idx_cols = ["0", "2"]
setnames!(x, idx_rows, 1);
setnames!(x, idx_cols, 2);
x
3×2 Named Array{Int64,2}
Dim1 ╲ Dim2 │ 0  2
────────────┼─────
0           │ 2  0
1           │ 0  1
2           │ 1  2

to

3×2 Named Array{Int64,2}
Dim1 ╲ Dim2 │ 0  1  2
────────────┼────────
0           │ 2  0  0
1           │ 0  0  1
2           │ 1  0  2

With Python Pandas DataFrame I do this using

df = df.loc[idx, idx.copy()].fillna(0)

But I haven’t found a correct way to do this with Julia.

I wonder if handling this is out of the scope of NamedArrays and if I should use a data structure handling NaN just for this ?

Kind regards

Pinging @davidavdav @nalimilan

1 Like

Hello,

This looks a bit outside the scope of NamedArrays, I suppose you can always construct a new array from the old one and insert a new column, and fix the names, but a one-line would start to look pretty ugly I suppose. The scope of NamedArrays is to be able to do what you can do with an Array, it is just that you can have names on the various rows/cols/… and the dimensions.

Would it help if there is an export to DataFrames, so that you can do such manipulation within that structure—that package should target to be more like Python pandas and R dataframes, I suppose.

—david

Hello,

Thanks for your answer.
A workaround (instead of converting to DataFrame) could be to create a
NamedArray filled with zeros and to add (in place) original NamedArray to
this one (despite size are differents but given the fact that keys in
original NamedArray also exists in new (and bigger) NamedArray.

About converting to DataFrame you might have a look at
https://github.com/davidanthoff/IterableTables.jl/issues/65 and
https://github.com/davidavdav/NamedArrays.jl/issues/55

Kind regards