Skip to content

BUG: Dropping element from unique IntervalIndex raises InvalidIndexError: Reindexing only valid with uniquely valued Index objects #52245

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

Open
3 tasks done
SlowMo24 opened this issue Mar 27, 2023 · 1 comment
Labels
Bug Interval Interval data type Timestamp pd.Timestamp and associated methods

Comments

@SlowMo24
Copy link

SlowMo24 commented Mar 27, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

data = [pd.Interval(pd.Timestamp('2020-05-26'), pd.Timestamp('2020-05-27')),
        pd.Interval(pd.Timestamp('2020-05-27'), pd.Timestamp('2020-05-27'))]

index = pd.IntervalIndex(data)

index.drop(index[0])
Traceback (most recent call last):
  File "report.py", line 10, in <module>
    index.drop(index[0])
  File ".venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 6930, in drop
    indexer = self.get_indexer_for(labels)
  File "/.venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 6052, in get_indexer_for
    return self.get_indexer(target)
  File ".venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3973, in get_indexer
    return self._get_indexer(target, method, limit, tolerance)
  File ".venv/lib/python3.10/site-packages/pandas/core/indexes/interval.py", line 685, in _get_indexer
    indexer = self._get_indexer_unique_sides(target)
  File ".venv/lib/python3.10/site-packages/pandas/core/indexes/interval.py", line 738, in _get_indexer_unique_sides
    right_indexer = self.right.get_indexer(target.right)
  File ".venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3904, in get_indexer
    raise InvalidIndexError(self._requires_unique_msg)
pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

Issue Description

Dropping an element from an IntervalIndex containing zero-length intervals raises an InvalidIndexError claiming Reindexing only valid with uniquely valued Index objects. Yet, index.is_unique returns true.

This sees to be related to #49581 . This is different from #38051 because it does not contain duplicates.

Expected Behavior

The first element of the index should be dropped without complaining.

Alternatively update the documentation to say when and why errors can be expected and what exactly happens if errors=ignore is supplied. Also reference that option in the InvalidIndexError message.

Installed Versions

.venv/lib/python3.10/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.") INSTALLED VERSIONS ------------------ commit : 2e218d1 python : 3.10.10.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-67-generic Version : #74~20.04.1-Ubuntu SMP Wed Feb 22 14:52:34 UTC 2023 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8 pandas : 1.5.3 numpy : 1.24.2 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 65.5.0 pip : 22.3.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.7.1 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : 2023.2
@SlowMo24 SlowMo24 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 27, 2023
@ltauro
Copy link

ltauro commented Mar 28, 2023

take

@DeaMariaLeon DeaMariaLeon added Interval Interval data type Timestamp pd.Timestamp and associated methods and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 30, 2023
@ltauro ltauro removed their assignment Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Interval Interval data type Timestamp pd.Timestamp and associated methods
Projects
None yet
Development

No branches or pull requests

3 participants