Skip to content
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

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

Closed
makbigc opened this issue Mar 24, 2019 · 1 comment · Fixed by #27100
Closed

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

makbigc opened this issue Mar 24, 2019 · 1 comment · Fixed by #27100
Labels
Bug Interval Interval data type
Milestone

Comments

@makbigc
Copy link
Contributor

makbigc commented Mar 24, 2019

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()

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

@jschendel
Copy link
Member

jschendel commented Mar 25, 2019

Thanks, I can confirm this behavior on master.

For what it's worth, I've been rewriting the IntervalIndex indexing methods as part of #16316 and this issue is not present in my new implementation:

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

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

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

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

Overall that branch is still a WIP and going somewhat slowly but I should have it ready in time for 0.25.0. Would still take a fix in the meantime but be aware that it might get overwritten shortly thereafter.

@jschendel jschendel added Bug Interval Interval data type labels Mar 25, 2019
@jreback jreback added this to the 0.25.0 milestone Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Interval Interval data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants