Skip to content

Selecting with MultiIndex raises a TypeError #5953

Closed
@leroyvn

Description

@leroyvn

What happened: After updating xarray to v0.20.1, some of my multi-index-based selection code raises a TypeError.

What you expected to happen: Selection should work in the case I am considering.

Minimal Complete Verifiable Example:

import xarray as xr
import pandas as pd

da = xr.DataArray(data=[0, 1, 2, 3], dims=("x",)).reindex(
    {"x": pd.MultiIndex.from_product((["foo", "bar"], [0, 1]), names=("str", "int"))}
)
print(da.sel(x=("foo", 1)))

Expected output (what I get with xarray 0.19.0):

<xarray.DataArray ()>
array(1)
Coordinates:
    x        object ('foo', 1)

Actual output (with xarray 0.20.1):

Traceback (most recent call last):
  File "playgrounds/xarray_mwe.py", line 7, in <module>
    da.sel(x=("foo", 1))
  File "/Users/leroyv/miniforge3/envs/eradiate/lib/python3.7/site-packages/xarray/core/dataarray.py", line 1337, in sel
    **indexers_kwargs,
  File "/Users/leroyv/miniforge3/envs/eradiate/lib/python3.7/site-packages/xarray/core/dataset.py", line 2505, in sel
    self, indexers=indexers, method=method, tolerance=tolerance
  File "/Users/leroyv/miniforge3/envs/eradiate/lib/python3.7/site-packages/xarray/core/coordinates.py", line 422, in remap_label_indexers
    obj, v_indexers, method=method, tolerance=tolerance
  File "/Users/leroyv/miniforge3/envs/eradiate/lib/python3.7/site-packages/xarray/core/indexing.py", line 120, in remap_label_indexers
    idxr, new_idx = index.query(labels, method=method, tolerance=tolerance)
  File "/Users/leroyv/miniforge3/envs/eradiate/lib/python3.7/site-packages/xarray/core/indexes.py", line 235, in query
    label_value, method=method, tolerance=tolerance
TypeError: get_loc() got an unexpected keyword argument 'tolerance'

Anything else we need to know?: Nothing I'm aware of

Environment:

Output of xr.show_versions() (with xarray 0.19.0)

INSTALLED VERSIONS

commit: None
python: 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 05:57:50)
[Clang 11.1.0 ]
python-bits: 64
OS: Darwin
OS-release: 20.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: (None, 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1

xarray: 0.19.0
pandas: 1.3.4
numpy: 1.21.4
scipy: 1.7.1
netCDF4: 1.5.8
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: None
bottleneck: None
dask: 2021.11.0
distributed: 2021.11.0
matplotlib: 3.4.3
cartopy: None
seaborn: 0.11.2
numbagg: None
pint: 0.18
setuptools: 58.5.3
pip: 21.3.1
conda: None
pytest: 6.2.5
IPython: 7.29.0
sphinx: 4.2.0

Output of xr.show_versions() (with xarray 0.20.1)

INSTALLED VERSIONS

commit: None
python: 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 05:57:50)
[Clang 11.1.0 ]
python-bits: 64
OS: Darwin
OS-release: 20.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: (None, 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1

xarray: 0.20.1
pandas: 1.3.4
numpy: 1.21.4
scipy: 1.7.1
netCDF4: 1.5.8
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: None
bottleneck: None
dask: 2021.11.0
distributed: 2021.11.0
matplotlib: 3.4.3
cartopy: None
seaborn: 0.11.2
numbagg: None
fsspec: 2021.11.0
cupy: None
pint: 0.18
sparse: None
setuptools: 58.5.3
pip: 21.3.1
conda: None
pytest: 6.2.5
IPython: 7.29.0
sphinx: 4.2.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions