Skip to content

Commit c90243b

Browse files
meeseeksmachinejorisvandenbossche
authored andcommitted
Backport PR #27926: Fix regression in .ix fallback with IntervalIndex (#28028)
1 parent 312b3b4 commit c90243b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.25.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Indexing
8585
- Bug in partial-string indexing returning a NumPy array rather than a ``Series`` when indexing with a scalar like ``.loc['2015']`` (:issue:`27516`)
8686
- Break reference cycle involving :class:`Index` and other index classes to allow garbage collection of index objects without running the GC. (:issue:`27585`, :issue:`27840`)
8787
- Fix regression in assigning values to a single column of a DataFrame with a ``MultiIndex`` columns (:issue:`27841`).
88+
- Fix regression in ``.ix`` fallback with an ``IntervalIndex`` (:issue:`27865`).
8889
-
8990

9091
Missing

pandas/core/indexing.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,17 @@ def __getitem__(self, key):
123123
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
124124
try:
125125
values = self.obj._get_value(*key)
126-
except (KeyError, TypeError, InvalidIndexError):
126+
except (KeyError, TypeError, InvalidIndexError, AttributeError):
127127
# TypeError occurs here if the key has non-hashable entries,
128128
# generally slice or list.
129129
# TODO(ix): most/all of the TypeError cases here are for ix,
130130
# so this check can be removed once ix is removed.
131131
# The InvalidIndexError is only catched for compatibility
132132
# with geopandas, see
133133
# https://github.com/pandas-dev/pandas/issues/27258
134+
# TODO: The AttributeError is for IntervalIndex which
135+
# incorrectly implements get_value, see
136+
# https://github.com/pandas-dev/pandas/issues/27865
134137
pass
135138
else:
136139
if is_scalar(values):

pandas/tests/indexing/test_ix.py

+10
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,13 @@ def test_ix_duplicate_returns_series(self):
343343
r = df.ix[0.2, "a"]
344344
e = df.loc[0.2, "a"]
345345
tm.assert_series_equal(r, e)
346+
347+
def test_ix_intervalindex(self):
348+
# https://github.com/pandas-dev/pandas/issues/27865
349+
df = DataFrame(
350+
np.random.randn(5, 2),
351+
index=pd.IntervalIndex.from_breaks([-np.inf, 0, 1, 2, 3, np.inf]),
352+
)
353+
result = df.ix[0:2, 0]
354+
expected = df.iloc[0:2, 0]
355+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)