Assigning non-dimension coordinates does not conserve MultiIndex type
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened:
Assigning a new non-dimension coordinate of type MultiIndex to a Dataset/DataArray returns an object that has the new coordinate as type Object instead of MultiIndex.
What you expected to happen:
Adding a MultiIndex as a dimension coordinate to a Dataset keeps the type. It should be the same for non-dimension.
Minimal Complete Verifiable Example:
import numpy as np
import pandas as pd
import xarray as xr
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]])
ds.assign_coords({'z': idx}) # Conserves MultiIndex type
ds.assign_coords({'z': ('x', idx)}) # Does not conserve MultiIndex type
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal example using Dataset.assign_coords with a pandas MultiIndex, then trace the assign_coords path for tuple-based coordinates. Done means the ('x', idx) form preserves the MultiIndex type like the direct form, with regression coverage for both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100