Skip to content

Commit 9c9f8fa

Browse files
BUG: Using boolean keys to select a column (GH44322)
1 parent 573c063 commit 9c9f8fa

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Indexing
576576
- Bug in :meth:`Series.__setitem__` with an integer dtype other than ``int64`` setting with a ``range`` object unnecessarily upcasting to ``int64`` (:issue:`44261`)
577577
- Bug in :meth:`Series.__setitem__` with a boolean mask indexer setting a listlike value of length 1 incorrectly broadcasting that value (:issue:`44265`)
578578
- Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`DataFrame.iloc.__setitem__` with mixed dtypes sometimes failing to operate in-place (:issue:`44345`)
579-
-
579+
- Bug in :meth:`DataFrame.loc.__getitem__` incorrectly raising ``KeyError`` when selecting a single column with a boolean key (:issue:`44322`).
580580

581581
Missing
582582
^^^^^^^

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ def _validate_key(self, key, axis: int):
994994
# slice of labels (where start-end in labels)
995995
# slice of integers (only if in the labels)
996996
# boolean not in slice and with boolean index
997-
if isinstance(key, bool) and not is_bool_dtype(self.obj.index):
997+
if isinstance(key, bool) and not is_bool_dtype(self.obj._get_axis(axis)):
998998
raise KeyError(
999999
f"{key}: boolean label can not be used without a boolean index"
10001000
)

pandas/tests/indexing/test_loc.py

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ def test_column_types_consistent(self):
178178
)
179179
tm.assert_frame_equal(df, expected)
180180

181+
def test_loc_getitem_column_boolean_arg(self):
182+
# GH 44322
183+
df = DataFrame([[1]], columns=Index([False]))
184+
res = df.loc[:, False]
185+
exp = Series([1], name=False)
186+
tm.assert_series_equal(res, exp)
187+
181188

182189
class TestLoc2:
183190
# TODO: better name, just separating out things that rely on base class

0 commit comments

Comments
 (0)