From e8e192c1468009ed49100908979bfab2202dc208 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 15 Oct 2020 18:35:42 -0700 Subject: [PATCH 1/2] split off of 37150 --- doc/source/whatsnew/v1.2.0.rst | 1 + pandas/core/frame.py | 2 +- pandas/tests/frame/indexing/test_indexing.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 9fc094330fb36..1788b6f7f7fb8 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -400,6 +400,7 @@ Indexing - Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`) - 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`) - Bug in indexing with boolean masks on datetime-like values sometimes returning a view instead of a copy (:issue:`36210`) +- Bug in :meth:`DataFrame.__getitem__` and :meth:`DataFrame.loc.__getitem__` with :class:`IntervalIndex` columns and a numeric indexer (:issue:`26490`) Missing ^^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 702552c0f4205..ec631c2b2c430 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2946,7 +2946,7 @@ def __getitem__(self, key): # - the key itself is repeated (test on data.shape, #9519), or # - we have a MultiIndex on columns (test on self.columns, #21309) if data.shape[1] == 1 and not isinstance(self.columns, MultiIndex): - data = data[key] + data = data._ixs(0, axis=1) return data diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 507d01f5b900c..c097557b33f4e 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -2160,6 +2160,20 @@ def test_interval_index(self): result = df.loc[1, "A"] tm.assert_series_equal(result, expected) + def test_getitem_interval_index_partial_indexing(self): + # GH#36490 + df = pd.DataFrame( + np.ones((3, 4)), columns=pd.IntervalIndex.from_breaks(np.arange(5)) + ) + + expected = df.iloc[:, 0] + + res = df[0.5] + tm.assert_series_equal(res, expected) + + res = df.loc[:, 0.5] + tm.assert_series_equal(res, expected) + class TestDataFrameIndexingUInt64: def test_setitem(self, uint64_frame): From 90f2636b13624878fba28ff86e97a9d4f160b640 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 16 Oct 2020 11:04:49 -0700 Subject: [PATCH 2/2] Troubleshoot --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ec631c2b2c430..c61eb5003beb9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2946,7 +2946,8 @@ def __getitem__(self, key): # - the key itself is repeated (test on data.shape, #9519), or # - we have a MultiIndex on columns (test on self.columns, #21309) if data.shape[1] == 1 and not isinstance(self.columns, MultiIndex): - data = data._ixs(0, axis=1) + # GH#26490 using data[key] can cause RecursionError + data = data._get_item_cache(key) return data