You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a project that includes a process where I need to:
Stack a DataArray
Substitute the new MultiIndex with a 1D index
Execute some operations on the DataArray that don't affect the MultiIndex dimension
Restore the original MultiIndex
Unstack the DataArray to regain the original dimension/coordinates
Now, here's the caveat: If the MultiIndex is already a 1D index, the solution I've employed earlier (#7337 ) doesn't hold water.
For instance, when I generate a 3D DataArray with dimensions (time, lat, lon), stack it into a 2D version with dimensions (sample, feature) where sample = ('time', ) and feature = ('lat', 'lon'), a problem arises. After carrying out the operations, I reassign the original MultiIndex and unstack. While unstacking works well for feature, it fails for sample. The final output dimensions are (sample, lon, lat), but I need them to be (time, lon, lat).
I understand this might seem a bit unusual, but it is a part of a more extensive procedure. I know I could just write some code to handle this 1D MultiIndex case and rename instead of stack, and I will if there's no other way. However, I'm curious if anyone could shed light on the behaviour I'm currently observing.
Below is a minimal working example demonstrating the issue:
MWE
# Create a 3D DataArraydata=xr.DataArray(
np.random.rand(5, 2, 2),
dims=['time', 'lon', 'lat'],
coords={'time': np.arange(5), 'lon': np.arange(2), 'lat': np.arange(2)}
)
# Stack DataArray into a 2D version with dimension (sample x feature)# Samples = time, Features = (lon, lat)data_stacked=data.stack(sample=('time',), feature=('lon', 'lat'))
original_coords=data_stacked.coordsdata_stacked=data_stacked.drop_vars({'sample'}).assign_coords(sample=np.arange(5))
data_stacked=data_stacked.drop_vars({'feature'}).assign_coords(feature=np.arange(4))
# ... do some operations on data_stackeddata_stacked=data_stacked.assign_coords(feature=original_coords['feature']).set_index(feature=['lon', 'lat'])
data_stacked=data_stacked.assign_coords(sample=original_coords['sample']).set_index(sample=('time',))
data_reconstruced=data_stacked.unstack()
assertdata.dims==data_reconstruced.dims# raises an AssertionError
The assertion at the end raises an AssertionError. How can I fix this issue and ensure the dimensions after unstacking are the same as the original data?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a project that includes a process where I need to:
DataArray
MultiIndex
with a 1D indexDataArray
that don't affect theMultiIndex
dimensionMultiIndex
DataArray
to regain the original dimension/coordinatesNow, here's the caveat: If the
MultiIndex
is already a 1D index, the solution I've employed earlier (#7337 ) doesn't hold water.For instance, when I generate a 3D
DataArray
with dimensions(time, lat, lon)
, stack it into a 2D version with dimensions(sample, feature)
wheresample = ('time', )
andfeature = ('lat', 'lon')
, a problem arises. After carrying out the operations, I reassign the originalMultiIndex
and unstack. While unstacking works well for feature, it fails for sample. The final output dimensions are(sample, lon, lat)
, but I need them to be(time, lon, lat)
.I understand this might seem a bit unusual, but it is a part of a more extensive procedure. I know I could just write some code to handle this 1D
MultiIndex
case andrename
instead ofstack
, and I will if there's no other way. However, I'm curious if anyone could shed light on the behaviour I'm currently observing.Below is a minimal working example demonstrating the issue:
MWE
The assertion at the end raises an
AssertionError
. How can I fix this issue and ensure the dimensions after unstacking are the same as the original data?Beta Was this translation helpful? Give feedback.
All reactions