question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataArray.loc fails for duplicates where DataFrame works

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
hortacommented, Sep 11, 2018

Hi again. I’m working on a precise definition of xarray and indexing. I find the official one a bit hard to understand. It might help me come up with a reasonable way to handle duplicate indices. https://drive.google.com/file/d/1uJ_U6nedkNe916SMViuVKlkGwPX-mGK7/view?usp=sharing

0reactions
stale[bot]commented, Aug 12, 2020

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas drop_duplicates method not working on dataframe ...
drop_duplicates won't work with lists in your dataframe as the error message implies. However, you can drop duplicates on the dataframe casted as...
Read more >
Solve Pandas "ValueError: cannot reindex from a duplicate axis"
In this blog post I elaborate on the "ValueError: cannot reindex from a duplicate axis" error and discuss what you can do to...
Read more >
Indexing and selecting data - Xarray
As xarray objects can store coordinates corresponding to each dimension of an array, label-based indexing similar to pandas.DataFrame.loc is also possible.
Read more >
What's New — xarray 0.9.6+dev240.g5a28b89 documentation
Fixes to ensure xarray works properly with pandas 0.21: ... Better multi-index support in DataArray and Dataset sel() and loc() methods, which now...
Read more >
pandas.DataFrame.duplicated — pandas 1.5.2 documentation
pandas.DataFrame.duplicated# ... Return boolean Series denoting duplicate rows. Considering certain columns is optional. ... Determines which duplicates (if any) to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found