BUG: DataFrame.loc[*, label] fails with IntervalIndex · Issue #19977 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

Setup:

In [2]: ii = pd.interval_range(0, 4)

In [3]: df = pd.DataFrame(np.arange(12).reshape(4, 3), index=ii, columns=list('ABC'))

In [4]: df Out[4]: A B C (0, 1] 0 1 2 (1, 2] 3 4 5 (2, 3] 6 7 8 (3, 4] 9 10 11

Scalar and Interval objects raise an AttributeError:

In [5]: df.loc[0.5, 'A']

AttributeError: 'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'

In [6]: df.loc[pd.Interval(0, 1), 'A']

AttributeError: 'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'

It works fine if column labels aren't passed, or if a list-like is passed:

In [7]: df.loc[0.5] Out[7]: A 0 B 1 C 2 Name: (0, 1], dtype: int32

In [8]: df.loc[pd.Interval(0, 1)] Out[8]: A 0 B 1 C 2 Name: (0, 1], dtype: int32

In [9]: df.loc[0.5, ['A']] Out[9]: A 0 Name: (0, 1], dtype: int32

In [10]: df.loc[pd.Interval(0, 1), ['B', 'C']] Out[10]: B 1 C 2 Name: (0, 1], dtype: int32

Problem description

Indexing with loc fails.

Expected Output

I'd expect loc to return the appropriate value.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 0bfb61b
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+422.g0bfb61b
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.0
pandas_gbq: None
pandas_datareader: None