-
-
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
Assigning non-dimension coordinates does not conserve MultiIndex type #4791
Comments
unfortunately, that's expected behavior right now. We might change that with the index refactor (see #1603), but until then it is not possible to have an index on non-dimension coordinates (that's one of the differences between dimension coordinates and non-dimension coordinates). |
Note that during the index refactor we will probably depreciate the implicit creation of multi-index level coordinates when assigning a pandas At the same time we should provide alternative ways of reusing existing pandas multi-indexes more explicitly in xarray. |
It is now possible with the last release v2023.8.0 to assign coordinates created from a ds = xr.Dataset(
data_vars={'foo': (['x', 'y'], np.random.rand(2, 2))},
coords={'x': [1, 2], 'y': [1, 2]}
)
idx = pd.MultiIndex.from_arrays([['a', 'b'], [5, 6]])
idx_coords = xr.Coordinates.from_pandas_multiindex(idx, "x")
ds.assign_coords(idx_coords) This will overwrite the pre-existing The following is possible today, though (PandasMultiIndex + indexed non-dimension coordinate): ds.rename_vars(x="x_alt").assign_coords(idx_coords)
|
What happened:
Assigning a new non-dimension coordinate of type
MultiIndex
to aDataset/DataArray
returns an object that has the new coordinate as typeObject
instead ofMultiIndex
.What you expected to happen:
Adding a
MultiIndex
as a dimension coordinate to aDataset
keeps the type. It should be the same for non-dimension.Minimal Complete Verifiable Example:
Anything else we need to know?:
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.9 (default, Aug 31 2020, 17:10:11) [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: en
LOCALE: None.None
libhdf5: 1.10.4
libnetcdf: 4.7.3
xarray: 0.16.1
pandas: 1.1.3
numpy: 1.19.1
scipy: 1.5.2
netCDF4: 1.5.3
pydap: installed
h5netcdf: None
h5py: None
Nio: None
zarr: 2.6.1
cftime: 1.3.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.30.0
distributed: 2.30.1
matplotlib: 3.3.3
cartopy: 0.18.0
seaborn: 0.11.0
numbagg: None
pint: None
setuptools: 50.3.0.post20201006
pip: 20.2.4
conda: 4.9.2
pytest: None
IPython: 7.18.1
sphinx: 3.2.1
The text was updated successfully, but these errors were encountered: