Description
What happened?
Hi, thank you all for this amazing tool and its capabilities.
I noticed my code breaking from an update of xarray of v2022.3 to v2022.12
I wanted to select the latitude inside the MultiIndex position=(lat, lon) but in the new version it doesn't seem to exist when I overwrite an existing index. It works though, when the DataArray is created with the MultiIndex in the first place.
See the code example below. In the meantime I will use xarray v2022.3
Thank you!
What did you expect to happen?
In v2022.3 the output would be (selecting latitudes)
>>> da.lat
<xarray.DataArray 'lat' (position: 4)>
array([0.1, 0.2, 0.7, 0.9])
Coordinates:
* position (position) MultiIndex
- lat (position) float64 0.1 0.2 0.7 0.9
- lon (position) float64 0.1 0.2 0.7 0.9
Minimal Complete Verifiable Example
import xarray as xr
import numpy as np
import pandas as pd
print(xr.__version__)
def test_xarray_multiindex():
mda = xr.DataArray(np.random.rand(4),
coords={"position": pd.Index([0.1, 0.2, 0.7, 0.9], name='position')},
dims="position")
midx = pd.MultiIndex.from_arrays([[0.1, 0.2, 0.7, 0.9], [0.1, 0.2, 0.7, 0.9]],
names=("lat", "lon"))
mda['position'] = midx
print(mda.lat) # <-- raises Error in xarray 2022.12
test_xarray_multiindex()
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
AttributeError Traceback (most recent call last)
Cell In[1], line 16
13 print(mda)
14 print(mda.lat) # <-- raises Error in some xr versions
---> 16 test_xarray_multiindex()
Cell In[1], line 14, in test_xarray_multiindex()
12 mda['position'] = midx
13 print(mda)
---> 14 print(mda.lat)
File /home/swd/manual/nwp/2023.1/lib/python3.10/site-packages/xarray/core/common.py:268, in AttrAccessMixin.__getattr__(self, name)
266 with suppress(KeyError):
267 return source[name]
--> 268 raise AttributeError(
269 f"{type(self).__name__!r} object has no attribute {name!r}"
270 )
AttributeError: 'DataArray' object has no attribute 'lat'
Anything else we need to know?
No response
Environment
xarray: 2023.2.0
pandas: 1.5.3
numpy: 1.23.5
scipy: 1.10.0
netCDF4: 1.6.2
pydap: None
h5netcdf: 1.1.0
h5py: 3.8.0
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2023.2.0
distributed: 2023.2.0
matplotlib: 3.7.0
cartopy: 0.21.1
seaborn: None
numbagg: None
fsspec: 2023.1.0
cupy: None
pint: 0.20.1
sparse: None
flox: None
numpy_groupies: None
setuptools: 67.3.2
pip: 23.0
conda: None
pytest: None
mypy: None
IPython: 8.10.0
sphinx: None
Metadata
Metadata
Assignees
Type
Projects
Status