Skip to content

Commit 388ec49

Browse files
committed
BUG: DataFrame.__getitem__(number) with IntervalIndex columns pandas-dev#26490
1 parent 020040b commit 388ec49

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-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

404405
Missing
405406
^^^^^^^

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2946,7 +2946,7 @@ 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+
data = data._ixs(0, axis=1)
29502950

29512951
return data
29522952

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)