Skip to content

Commit f22e55d

Browse files
jbrockmendeljreback
authored andcommitted
BUG: RecursionError when selecting single column from IntervalIndex pandas-dev#26490 (pandas-dev#37152)
* split off of 37150 * Troubleshoot Co-authored-by: Jeff Reback <[email protected]>
1 parent b79d803 commit f22e55d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ Indexing
400400
- Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`)
401401
- Bug in :meth:`DataFrame.reset_index` was incorrectly raising a ``ValueError`` for input with a :class:`MultiIndex` with missing values in a level with ``Categorical`` dtype (:issue:`24206`)
402402
- Bug in indexing with boolean masks on datetime-like values sometimes returning a view instead of a copy (:issue:`36210`)
403+
- Bug in :meth:`DataFrame.__getitem__` and :meth:`DataFrame.loc.__getitem__` with :class:`IntervalIndex` columns and a numeric indexer (:issue:`26490`)
403404
- Bug in :meth:`Series.loc.__getitem__` with a non-unique :class:`MultiIndex` and an empty-list indexer (:issue:`13691`)
404405

405406
Missing

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2946,7 +2946,8 @@ def __getitem__(self, key):
29462946
# - the key itself is repeated (test on data.shape, #9519), or
29472947
# - we have a MultiIndex on columns (test on self.columns, #21309)
29482948
if data.shape[1] == 1 and not isinstance(self.columns, MultiIndex):
2949-
data = data[key]
2949+
# GH#26490 using data[key] can cause RecursionError
2950+
data = data._get_item_cache(key)
29502951

29512952
return data
29522953

pandas/tests/frame/indexing/test_indexing.py

+14
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,20 @@ def test_interval_index(self):
21602160
result = df.loc[1, "A"]
21612161
tm.assert_series_equal(result, expected)
21622162

2163+
def test_getitem_interval_index_partial_indexing(self):
2164+
# GH#36490
2165+
df = pd.DataFrame(
2166+
np.ones((3, 4)), columns=pd.IntervalIndex.from_breaks(np.arange(5))
2167+
)
2168+
2169+
expected = df.iloc[:, 0]
2170+
2171+
res = df[0.5]
2172+
tm.assert_series_equal(res, expected)
2173+
2174+
res = df.loc[:, 0.5]
2175+
tm.assert_series_equal(res, expected)
2176+
21632177

21642178
class TestDataFrameIndexingUInt64:
21652179
def test_setitem(self, uint64_frame):

0 commit comments

Comments
 (0)