Skip to content

DataArray.loc fails for duplicates where DataFrame works #2399

Closed
@horta

Description

@horta
import pandas as pd
import xarray as xr

df = pd.DataFrame(data=[0, 1], index=list("aa"))
da = xr.DataArray(df)
print(df.loc[list("a")])  # works
print(da.loc[{"dim_0": list("a")}])  # fails

Xarray fails with the exception

Traceback (most recent call last):
  File "session4.py", line 7, in <module>
    print(da.loc[{"dim_0": list("a")}])  # fails
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/dataarray.py", line 104, in __getitem__
    return self.data_array.sel(**key)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/dataarray.py", line 784, in sel
    indexers=indexers, drop=drop, method=method, tolerance=tolerance)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/dataset.py", line 1509, in sel
    self, indexers=indexers, method=method, tolerance=tolerance)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/coordinates.py", line 355, in remap_label_indexers
    obj, v_indexers, method=method, tolerance=tolerance
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/indexing.py", line 250, in remap_label_indexers
    dim, method, tolerance)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/indexing.py", line 186, in convert_label_indexer
    indexer = get_indexer_nd(index, label, method, tolerance)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/xarray/core/indexing.py", line 117, in get_indexer_nd
    flat_indexer = index.get_indexer(flat_labels, **kwargs)
  File "/usr/local/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3244, in get_indexer
    raise InvalidIndexError('Reindexing only valid with uniquely'
pandas.core.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

Output of xr.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 17.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

xarray: 0.10.8
pandas: 0.23.4
numpy: 1.15.1
scipy: 1.1.0
netCDF4: 1.4.1
h5netcdf: None
h5py: 2.8.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.19.0
distributed: 1.23.0
matplotlib: 2.2.3
cartopy: None
seaborn: 0.9.0
setuptools: 40.2.0
pip: 18.0
conda: 4.5.11
pytest: 3.7.3
IPython: 6.5.0
sphinx: 1.7.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions