Here is a toy example but I have 2 dataframes; (1) rows=samples, cols=attributes; and (2) rows=samples, cols=metadata-fields.
I want to concat or merge to create 3-dimensional xr.DataArray. I've done this multiple times but I can't figure out why it's not working in this case? I want to concat along the patient_id axis to have a 3D xr.DataArray.
Why isn't xr.concat building the 3-dimensional DataArray? I think I'm incorrectly using the dim argument since it is supposed to concat along a new-axis but is there a way to do this along an existing axis?
I'm trying to use the method from Create DataArray from Dict of 2D DataFrames/Arrays but it isn't working. I got merge to work but it puts it into a DataSet w/ 2 data variables
np.random.seed(0)
patient_ids = ["patient_%d"%_ for _ in range(42)]
attr_ids = ["attr_%d"%_ for _ in range(481)]
meta_ids = ["meta_%d"%_ for _ in range(32)]
DA_A = xr.DataArray(pd.DataFrame(np.random.random((42,481)),
index=patient_ids,
columns=attr_ids),
dims=["patient_id","attribute"])
DA_B = xr.DataArray(pd.DataFrame(np.random.random((42,32)),
index=patient_ids,
columns=meta_ids),
dims=["patient_id","metadata"])
DA_A.coords
# Coordinates:
# * patient_id (patient_id) object 'patient_0' 'patient_1' 'patient_2' ...
# * attribute (attribute) object 'attr_0' 'attr_1' 'attr_2' 'attr_3' ...
DA_B.coords
# Coordinates:
# * patient_id (patient_id) object 'patient_0' 'patient_1' 'patient_2' ...
# * metadata (metadata) object 'meta_0' 'meta_1' 'meta_2' 'meta_3' ...
xr.concat([DA_A, DA_B], dim="patient_id")
# KeyError: 'attribute'
concat. The error you are getting is telling you thatDA_Bdoesn't have theattributedimension. Based on what I see you trying to do, aDatasetwith shared/aligned dimensions (patient_id, attribute, metadata) is probably what you want.