Skip to content

BUG: fix failing DataFrame.loc when indexing with an IntervalIndex #22576

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

Merged
merged 4 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ Indexing
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)
- Bug where mixed indexes wouldn't allow integers for ``.at`` (:issue:`19860`)
- ``Float64Index.get_loc`` now raises ``KeyError`` when boolean key passed. (:issue:`19087`)
- Bug in :meth:`DataFrame.loc` when indexing with an :class:`IntervalIndex` (:issue:`19977`)

Missing
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ def __getitem__(self, key):
try:
if self._is_scalar_access(key):
return self._getitem_scalar(key)
except (KeyError, IndexError):
except (KeyError, IndexError, AttributeError):
pass
return self._getitem_tuple(key)
else:
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3099,6 +3099,28 @@ def test_type_error_multiindex(self):
result = dg['x', 0]
assert_series_equal(result, expected)

def test_interval_index(self):
# GH 19977
index = pd.interval_range(start=0, periods=3)
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
index=index,
columns=['A', 'B', 'C'])

expected = 1
result = df.loc[0.5, 'A']
assert_almost_equal(result, expected)

index = pd.interval_range(start=0, periods=3, closed='both')
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
index=index,
columns=['A', 'B', 'C'])

index_exp = pd.interval_range(start=0, periods=2,
freq=1, closed='both')
expected = pd.Series([1, 4], index=index_exp, name='A')
result = df.loc[1, 'A']
assert_series_equal(result, expected)


class TestDataFrameIndexingDatetimeWithTZ(TestData):

Expand Down