Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[BUG] IntervalIndex.get_loc error for decreasing index #25860

Copy link
Copy link
@makbigc

Description

@makbigc
Issue body actions

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd                                                     

In [2]: intv1 = pd.IntervalIndex.from_arrays([1, 3, 5], [2, 4, 6])              

In [3]: intv1                                                                   
Out[3]: 
IntervalIndex([(1, 2], (3, 4], (5, 6]],
              closed='right',
              dtype='interval[int64]')

In [4]: intv1.get_loc(intv1[0])                                                 
Out[4]: 0

In [5]: intv2 = pd.IntervalIndex.from_arrays([4, 2, 1], [5, 3, 2])              

In [6]: intv2                                                                   
Out[6]: 
IntervalIndex([(4, 5], (2, 3], (1, 2]],
              closed='right',
              dtype='interval[int64]')

In [7]: intv2.get_loc(intv2[0])                                                 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-00c58bb4fb1e> in <module>
----> 1 intv2.get_loc(intv2[0])

~/Code/pandas/pandas/core/indexes/interval.py in get_loc(self, key, method)
    777                 return slice(start, stop)
    778             else:
--> 779                 raise KeyError(original_key)
    780 
    781         else:

KeyError: Interval(4, 5, closed='right')

Problem description

This IntervalIndex is non-overlapping_monotonic. IntervalIndex.get_loc runs in KeyError, even the key is its first element.

After start, stop = self._find_non_overlapping_monotonic_bounds(key), start and stop get the same value. Thereafter, no conditional handles this case.

Expected Outcome

In [5]: intv2 = pd.IntervalIndex.from_arrays([4, 2, 1], [5, 3, 2])

In [6]: intv2
Out[6]:
IntervalIndex([(4, 5], (2, 3], (1, 2]],
closed='right',
dtype='interval[int64]')

In [7]: intv2.get_loc(intv2[0])
Out[7]: 0

Output of pd.show_versions()

Details

INSTALLED VERSIONS

commit: 0198873
python: 3.7.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.0-16-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_HK.UTF-8
LOCALE: en_HK.UTF-8

pandas: 0.25.0.dev0+308.g01988735f
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.