Skip to content

Commit 5d5a84a

Browse files
punndcoder28proost
authored andcommitted
minor inconsistency in Categorical.remove_categories error message (pandas-dev#28677)
1 parent 16311d5 commit 5d5a84a

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ Categorical
260260
- Bug where :func:`merge` was unable to join on categorical and extension dtype columns (:issue:`28668`)
261261
- :meth:`Categorical.searchsorted` and :meth:`CategoricalIndex.searchsorted` now work on unordered categoricals also (:issue:`21667`)
262262
- Added test to assert roundtripping to parquet with :func:`DataFrame.to_parquet` or :func:`read_parquet` will preserve Categorical dtypes for string types (:issue:`27955`)
263-
-
263+
- Changed the error message in :meth:`Categorical.remove_categories` to always show the invalid removals as a set (:issue:`28669`)
264264

265265

266266
Datetimelike

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ def remove_categories(self, removals, inplace=False):
11241124

11251125
# GH 10156
11261126
if any(isna(removals)):
1127-
not_included = [x for x in not_included if notna(x)]
1127+
not_included = {x for x in not_included if notna(x)}
11281128
new_categories = [x for x in new_categories if notna(x)]
11291129

11301130
if len(not_included) != 0:

pandas/tests/arrays/categorical/test_api.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -339,9 +341,13 @@ def test_remove_categories(self):
339341
tm.assert_categorical_equal(cat, new)
340342
assert res is None
341343

342-
# removal is not in categories
343-
with pytest.raises(ValueError):
344-
cat.remove_categories(["c"])
344+
@pytest.mark.parametrize("removals", [["c"], ["c", np.nan], "c", ["c", "c"]])
345+
def test_remove_categories_raises(self, removals):
346+
cat = Categorical(["a", "b", "a"])
347+
message = re.escape("removals must all be in old categories: {'c'}")
348+
349+
with pytest.raises(ValueError, match=message):
350+
cat.remove_categories(removals)
345351

346352
def test_remove_unused_categories(self):
347353
c = Categorical(["a", "b", "c", "d", "a"], categories=["a", "b", "c", "d", "e"])

0 commit comments

Comments
 (0)