Skip to content

Commit 83fabfb

Browse files
authored
BUG: df.drop not separating missing labels with commas (#42938)
1 parent 45fd72a commit 83fabfb

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Indexing
242242
- Bug in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`)
243243
- Bug in :meth:`Index.get_indexer_non_unique` when index contains multiple ``np.nan`` (:issue:`35392`)
244244
- Bug in :meth:`DataFrame.query` did not handle the degree sign in a backticked column name, such as \`Temp(°C)\`, used in an expression to query a dataframe (:issue:`42826`)
245+
- Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`)
245246
-
246247

247248
Missing

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6290,7 +6290,7 @@ def drop(self, labels, errors: str_t = "raise") -> Index:
62906290
mask = indexer == -1
62916291
if mask.any():
62926292
if errors != "ignore":
6293-
raise KeyError(f"{labels[mask]} not found in axis")
6293+
raise KeyError(f"{list(labels[mask])} not found in axis")
62946294
indexer = indexer[~mask]
62956295
return self.delete(indexer)
62966296

pandas/tests/frame/methods/test_drop.py

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def test_drop(self):
130130
with pytest.raises(KeyError, match=r"\['C'\] not found in axis"):
131131
simple.drop(["A", "C"], axis=1)
132132

133+
# GH 42881
134+
with pytest.raises(KeyError, match=r"\['C', 'D', 'F'\] not found in axis"):
135+
simple.drop(["C", "D", "F"], axis=1)
136+
133137
# errors = 'ignore'
134138
tm.assert_frame_equal(simple.drop(5, errors="ignore"), simple)
135139
tm.assert_frame_equal(

0 commit comments

Comments
 (0)