Skip to content

Commit 9b402bb

Browse files
Adjustments to comments.
1 parent 7f35cf8 commit 9b402bb

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ Indexing
642642
- Bug in setting ``numpy.timedelta64`` values into an object-dtype :class:`Series` using a boolean indexer (:issue:`39488`)
643643
- Bug in setting numeric values into a into a boolean-dtypes :class:`Series` using ``at`` or ``iat`` failing to cast to object-dtype (:issue:`39582`)
644644
- Bug in :meth:`DataFrame.loc.__setitem__` when setting-with-expansion incorrectly raising when the index in the expanding axis contains duplicates (:issue:`40096`)
645-
- Bug in :meth:`DataFrame.loc` incorrectly allowing the lookup of boolean labels and slices (:issue:`20432`)
645+
- Bug in :meth:`DataFrame.loc` incorrectly matching non-boolean index elements (:issue:`20432`)
646646

647647
Missing
648648
^^^^^^^

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,12 @@ def _validate_key(self, key, axis: int):
966966
# slice of integers (only if in the labels)
967967
# boolean not in slice and with boolean index
968968
if isinstance(key, bool) and not is_bool_dtype(self.obj.index):
969-
raise KeyError("Boolean label can not be used without a boolean index")
969+
raise KeyError(f"{key}: boolean label can not be used without a boolean index")
970970

971971
if isinstance(key, slice) and (
972972
isinstance(key.start, bool) or isinstance(key.stop, bool)
973973
):
974-
raise TypeError("Boolean values can not be used in a slice")
974+
raise TypeError(f"{key}: boolean values can not be used in a slice")
975975

976976
def _has_valid_setitem_indexer(self, indexer) -> bool:
977977
return True

pandas/tests/indexing/test_loc.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -2117,23 +2117,25 @@ def test_loc_getitem_slice_columns_mixed_dtype(self):
21172117

21182118

21192119
class TestLocBooleanLabelsAndSlices(Base):
2120-
def test_loc_bool_incompatible_index_raises(self, index, frame_or_series):
2120+
@pytest.mark.parametrize("bool_value", [True, False])
2121+
def test_loc_bool_incompatible_index_raises(self, index, frame_or_series, bool_value):
21212122
# GH20432
2122-
message = "Boolean label can not be used without a boolean index"
2123+
message = f"{bool_value}: boolean label can not be used without a boolean index"
21232124
if index.inferred_type != "boolean":
21242125
obj = frame_or_series(index=index, dtype="object")
21252126
with pytest.raises(KeyError, match=message):
2126-
obj.loc[True]
2127+
obj.loc[bool_value]
21272128

2128-
def test_loc_bool_should_not_raise(self, frame_or_series):
2129+
@pytest.mark.parametrize("bool_value", [True, False])
2130+
def test_loc_bool_should_not_raise(self, frame_or_series, bool_value):
21292131
obj = frame_or_series(
21302132
index=Index([True, False], dtype="boolean"), dtype="object"
21312133
)
2132-
obj.loc[True]
2134+
obj.loc[bool_value]
21332135

21342136
def test_loc_bool_slice_raises(self, index, frame_or_series):
21352137
# GH20432
2136-
message = "Boolean values can not be used in a slice"
2138+
message = "slice\(True, False, None\): boolean values can not be used in a slice"
21372139
obj = frame_or_series(index=index, dtype="object")
21382140
with pytest.raises(TypeError, match=message):
21392141
obj.loc[True:False]

0 commit comments

Comments
 (0)