-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't unstack concatenated DataArrays #7076
Comments
Hi @DWesl, Your example is working with the # after #6889
arr2.unstack("index0")
# <xarray.DataArray (index2: 2, level_0: 3, level_1: 5)>
# array([[[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14]],
#
# [[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14]]])
# Coordinates:
# * level_0 (level_0) int64 0 1 2
# * level_1 (level_1) int64 0 1 2 3 4
# Dimensions without coordinates: index2 |
Fix confirmed, thank you. |
I got a bug very similar to what was initially report with the following minimal example (xarray version 2022.11.0) with the error:
It is working fine with my previous version (xarray 0.16.0) import xarray as xr
ds = xr.Dataset(
data_vars=dict(
mydata = ( ['across', 'along'], [[0,1],[2,3]])
)
)
stacked = ds.stack(z=("across", "along"))
newlist = [None] * stacked.z.size
for ii, zindex in enumerate(stacked.z.data):
newlist[ii] = stacked.mydata.sel(z=zindex)
newds = xr.concat(newlist, dim='z')
newds['z'] = stacked.z
print('xarray version:' + xr.__version__)
newds.unstack(dim='z') If I do Many thanks for your help, |
@ACHMartin the issue is when you do I think that in other places we still do support it with a deprecation notice, but apparently in your example this is not the case. I don't know exactly what is your real problem, but from now on you should avoid implicitly assign a multi-index with |
Many thanks for the explanation and the rapid answer. I am not very familiar with multi-index. My real problem works with a DataSet with different sort of variables:
I don't know where I should apply
Which is obvious as across and along variables disappeared. Thank you, I will dig in this direction why it happens. However, it works perfectly with the minimal example. |
Are we raising a warning here? |
I didn't see any warning for this |
Thanks @benbovy, my issue is now resolved, once I have enabled the coords to follow. |
…y one multi-index: ('z',)" occuring in level2/wind_current_retrieval with version xarray 2022.11.0 (was working in xarray v0.16.0) on unstack on 'z'. Explanation of the pb here: pydata/xarray#7076 (comment) Push in cost_function/find_minima the level1 coords to the output
Hi @benbovy, my code works for 2 dimensions, but it doesn't with 1D. I think the error comes from I created a Pandas-MultiIndex "MultiIndex([((0,), (1,))], names=['z', 'across'])" (see code just below), but it is not exactly the same one as the one created by import pandas as pd
tuples = [(0,),(1,)],
index = pd.MultiIndex.from_tuples(tuples, names=["z","across"])
index My modified minimal example is as below: import xarray as xr
ds = xr.Dataset(
data_vars=dict(
mydata = ( ['across', 'along'], [[0,1],[2,3]])
),
coords=dict( # <- changed
across=[0,1], # <- changed
along=[0,1], # <- changed
), # <- changed
)
stacked = ds.isel(along=0).stack(z=["across"]) # <- changed
newlist = [None] * stacked.z.size
for ii, zindex in enumerate(stacked.z.data):
newlist[ii] = stacked.mydata.sel(z=zindex)
newds = xr.concat(newlist, dim='z')
newds = newds.set_index(z=['across']) # <- changed
newds.unstack(dim='z') # Fail with ValueError: cannot unstack dimensions that do not have exactly one multi-index: ('z',) |
…y one multi-index: ('z',)" occuring in level2/wind_current_retrieval with version xarray 2022.11.0 (was working in xarray v0.16.0) on unstack on 'z'. Explanation of the pb here: pydata/xarray#7076 (comment) Push in cost_function/find_minima the level1 coords to the output
Is the underlying cause of this a dupe of #7148? |
I don't think it is a duplicate. I don't know what @ACHMartin's last minimal example is trying to do, but now the best way to assign a pandas multi-index to a Xarray object is like this: index = pd.MultiIndex.from_tuples(..., names=["across"])
coords = xr.Coordinates.from_pandas_multiindex(index, dim="z")
ds = xr.Dataset(coords=coords) It looks like Regarding assignment of a new coordinate to an existing multi-index coordinate (also reported here above), if we still don't raise an error or a warning we should probably do so. |
What happened?
I had a collection of
DataArray
s with a stacked dimension (dimension whose corresponding index is aMultiIndex
). I concatenated them into a singleDataArray
, then tried to unstack the stacked dimension, which failed. Performing the operations in the other order works (unstacking eachDataArray
, then concatenating the unstacked arrays).What did you expect to happen?
I expected that concatenating the arrays then unstacking them would produce the same array as unstacking them then concatenating them, but with the possibility of saving the intermediate concatenated-but-still-stacked
DataArray
for later use as a template.Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
The eventual problem to which I wish to apply the solution has two stacked dimensions rather than one, but that's likely irrelevant.
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.76.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1
xarray: 2022.6.0
pandas: 1.4.2
numpy: 1.22.3
scipy: 1.8.0
netCDF4: 1.6.0
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.5.1.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: 3.2.1.post0
bottleneck: 1.3.5
dask: 2022.7.1
distributed: 2022.7.1
matplotlib: 3.5.1
cartopy: 0.20.3
seaborn: 0.12.0
numbagg: None
fsspec: 2022.5.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 61.3.1
pip: 22.0.4
conda: 4.14.0
pytest: 7.1.3
IPython: None
sphinx: None
The text was updated successfully, but these errors were encountered: