unstack() appears to fail to unstack a MultiIndex under specific circumstances. What's going on? #5989
-
Hi all. First, let me say that it's entirely possible that I'm simply doing something wrong. Any advice/help is appreciated! Problem description I'm working with some gridded data (i.e. with two dimensions, Here's what I'm trying to do, specifically: I need to combine a few 1D DataArrays into an existing Dataset that uses a MultiIndex. The idea is to combine all the 1D DataArrays together and then use What happened: The What you expected to happen: I expected the Try it out: if you comment out one of the lines indicated below, then the code returns a two-dimensional Dataset. Minimal Complete Verifiable Example: # import packages
import xarray as xr
import numpy as np
# generate random example data on a 2D "grid"
np.random.seed(0)
temperature = 15 + 8 * np.random.randn(2, 2)
precipitation = 10 * np.random.rand(2, 2)
lon = [[-99.83, -99.32], [-99.79, -99.23]]
lat = [[42.25, 42.21], [42.63, 42.59]]
# create DataArray using the "temperature" data
ds = xr.Dataset(
data_vars=dict(
temperature=(["x", "y"], temperature),
precipitation=(["x", "y"], precipitation),
),
coords=dict(
lon=(["x", "y"], lon),
lat=(["x", "y"], lat),
),
attrs=dict(description="Weather related data."),
)
# stack the Dataset
ds_stacked = ds.stack(z=("x","y"))
ds_stacked
# create 1D array of labels (to be associated with each lat/lon pair)
d1 = xr.DataArray(
data=np.random.randint(low=0, high=2, size=(4,)),
dims=["z"],
attrs=dict(
description="Label",
),
)
# create 1D array of labels (to be associated with each lat/lon pair)
d2 = xr.DataArray(
data=np.random.randn(4,),
dims=["z"],
attrs=dict(
description="Posteriors",
),
)
# assign the labels in d1 to da_stacked
ds_stacked['label'] = d1 #<-- comment out either this line...
ds_stacked['post'] = d2 #<-- or this line...and the unstack() command does what I expect it to do!
ds_stacked
# finally, unstack
ds_final = ds_stacked.unstack() # <-- this should unstack the "z" MultiIndex coordinate.
ds_final Anything else we need to know?: I'm reasonably convinced that this method will do what I need it to do, if I can get it to work. That being said, I'm open to other alternative approaches to this problem. : ) Any thoughts or suggestions? Environment: Output of xr.show_versions()INSTALLED VERSIONScommit: None xarray: 0.20.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Assigning Before: <xarray.Dataset>
Dimensions: (z: 4)
Coordinates:
* z (z) MultiIndex
- x (z) int64 0 0 1 1
- y (z) int64 0 1 0 1 After: <xarray.Dataset>
Dimensions: (z: 4)
Coordinates:
* z (z) object (0, 0) (0, 1) (1, 0) (1, 1)
lon (z) float64 -99.83 -99.32 -99.79 -99.23
lat (z) float64 42.25 42.21 42.63 42.59 I am not entirely why this happens... But to work around this issue you have two possibilities: # assign a raw array
ds_stacked['label'] = ("z", d1.data)
# add the correct coords
d2 = d2.assign_coords(z=ds_stacked.z)
ds_stacked['post'] = d2 |
Beta Was this translation helpful? Give feedback.
Assigning
d1
ord1
yourds_stacked
looses theMultiIndex
compareBefore:
After:
I am not entirely why this happens... But to work around this issue you have two possibilities: