issues: 1352621981
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1352621981 | I_kwDOAMm_X85Qn1-d | 6959 | Assigning coordinate level to MultiIndex fails if MultiIndex only has one level | 20617032 | closed | 0 | 4160723 | 0 | 2022-08-26T18:48:18Z | 2022-09-27T10:35:39Z | 2022-09-27T10:35:39Z | NONE | What happened?This issue originates from this discussion where I was trying to figure out the best way to replace coordinate values in a MultiIndex level. I found that removing the level with What did you expect to happen?I expect that removing and replacing a coordinate level would work the same independent of the number of levels in the MultiIndex. Minimal Complete Verifiable Example```Python import numpy as np import pandas as pd import xarray as xr Replace the coordinates in level 'one'. This works as expected.midx = pd.MultiIndex.from_product([[0,1,2], [3, 4], [5,6]], names=("one", "two","three")) mda = xr.DataArray(np.random.rand(12, 3), [("x", midx), ("y", range(3))]) new_coords = mda.coords['one'].values*2 mda.reset_index('one', drop=True).assign_coords(one= ('x',new_coords)).set_index(x='one',append=True) #Works Drop the two level before had such that the intermediate state has a multindexwith only the 'three' level, this throws a ValueErrormda.reset_index('two',drop=True).reset_index('one', drop=True).assign_coords(one= ('x',new_coords)) #ValueError We can intialize a data array with only two levels and only drop the 'one'level, which gives the same ValueError. This shows that the problem is notdue to something with dropping the 'two' level above, but something inherentto dropping to a state with only one multinddex levelmidx = pd.MultiIndex.from_product([[0,1,2], [3, 4]], names=("one", "two")) mda = xr.DataArray(np.random.rand(6, 2), [("x", midx), ("y", range(2))]) new_coords = mda.coords['one'].values*2 mda.reset_index('one', drop=True).assign_coords(one= ('x',new_coords)).set_index(x='one',append=True) #ValueError ``` MVCE confirmation
Relevant log output```Python First example, starting from 3 level multiindex and dropping two levelsValueError Traceback (most recent call last) c:\Users\aspit\Git\Learn\xarray\replace_coord_issue.py in <module> 15 # Drop the two level before had such that the intermediate state has a multindex 16 # with only the 'three' level, this throws a ValueError ---> 17 mda.reset_index('two',drop=True).reset_index('one', drop=True).assign_coords(one= ('x',new_coords)) c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\common.py in assign_coords(self, coords, **coords_kwargs) 590 data = self.copy(deep=False) 591 results: dict[Hashable, Any] = self._calc_assign_results(coords_combined) --> 592 data.coords.update(results) 593 return data 594 c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\coordinates.py in update(self, other) 160 other_vars = getattr(other, "variables", other) 161 self._maybe_drop_multiindex_coords(set(other_vars)) --> 162 coords, indexes = merge_coords( 163 [self.variables, other_vars], priority_arg=1, indexes=self.xindexes 164 ) c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in merge_coords(objects, compat, join, priority_arg, indexes, fill_value) 564 collected = collect_variables_and_indexes(aligned) 565 prioritized = _get_priority_vars_and_indexes(aligned, priority_arg, compat=compat) --> 566 variables, out_indexes = merge_collected(collected, prioritized, compat=compat) 567 return variables, out_indexes 568 c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in merge_collected(grouped, prioritized, compat, combine_attrs, equals) 252 253 _assert_compat_valid(compat) --> 254 _assert_prioritized_valid(grouped, prioritized) 255 256 merged_vars: dict[Hashable, Variable] = {} c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in _assert_prioritized_valid(grouped, prioritized) 199 common_names_str = ", ".join(f"{k!r}" for k in common_names) 200 index_names_str = ", ".join(f"{k!r}" for k in index_coord_names) --> 201 raise ValueError( 202 f"cannot set or update variable(s) {common_names_str}, which would corrupt " 203 f"the following index built from coordinates {index_names_str}:\n" ValueError: cannot set or update variable(s) 'one', which would corrupt the following index built from coordinates 'x', 'one', 'three': <xarray.core.indexes.PandasMultiIndex object at 0x00000225AA4B5200> Second Example Starting from two level multindex and dropping one levelValueError Traceback (most recent call last) c:\Users\aspit\Git\Learn\xarray\replace_coord_issue.py in <module> 11 12 new_coords = mda.coords['one'].values*2 ---> 13 mda.reset_index('one', drop=True).assign_coords(one= ('x',new_coords)).set_index(x='one',append=True) c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\common.py in assign_coords(self, coords, **coords_kwargs) 590 data = self.copy(deep=False) 591 results: dict[Hashable, Any] = self._calc_assign_results(coords_combined) --> 592 data.coords.update(results) 593 return data 594 c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\coordinates.py in update(self, other) 160 other_vars = getattr(other, "variables", other) 161 self._maybe_drop_multiindex_coords(set(other_vars)) --> 162 coords, indexes = merge_coords( 163 [self.variables, other_vars], priority_arg=1, indexes=self.xindexes 164 ) c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in merge_coords(objects, compat, join, priority_arg, indexes, fill_value) 564 collected = collect_variables_and_indexes(aligned) 565 prioritized = _get_priority_vars_and_indexes(aligned, priority_arg, compat=compat) --> 566 variables, out_indexes = merge_collected(collected, prioritized, compat=compat) 567 return variables, out_indexes 568 c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in merge_collected(grouped, prioritized, compat, combine_attrs, equals) 252 253 _assert_compat_valid(compat) --> 254 _assert_prioritized_valid(grouped, prioritized) 255 256 merged_vars: dict[Hashable, Variable] = {} c:\Users\aspit\anaconda3\envs\dataanalysis\lib\site-packages\xarray\core\merge.py in _assert_prioritized_valid(grouped, prioritized) 199 common_names_str = ", ".join(f"{k!r}" for k in common_names) 200 index_names_str = ", ".join(f"{k!r}" for k in index_coord_names) --> 201 raise ValueError( 202 f"cannot set or update variable(s) {common_names_str}, which would corrupt " 203 f"the following index built from coordinates {index_names_str}:\n" ValueError: cannot set or update variable(s) 'one', which would corrupt the following index built from coordinates 'x', 'one', 'two': <xarray.core.indexes.PandasMultiIndex object at 0x00000225AA53C9E0> ``` Anything else we need to know?No response Environment
INSTALLED VERSIONS
------------------
commit: None
python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:15:42) [MSC v.1916 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('English_United States', '1252')
libhdf5: 1.12.1
libnetcdf: 4.8.1
xarray: 2022.6.0
pandas: 1.3.4
numpy: 1.21.4
scipy: 1.7.3
netCDF4: 1.5.8
pydap: None
h5netcdf: 1.0.2
h5py: 3.7.0
Nio: None
zarr: None
cftime: 1.5.1.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.5
dask: 2022.02.1
distributed: 2022.2.1
matplotlib: 3.4.3
cartopy: None
seaborn: None
numbagg: None
fsspec: 2022.7.1
cupy: None
pint: 0.18
sparse: None
flox: None
numpy_groupies: None
setuptools: 59.1.0
pip: 21.3.1
conda: None
pytest: 6.2.5
IPython: 7.29.0
sphinx: None
|
{ "url": "https://api.github.com/repos/pydata/xarray/issues/6959/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |