Skip to content

Commit 89b510c

Browse files
authored
PERF: remove categories (#51344)
1 parent c7fa611 commit 89b510c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pandas/core/arrays/categorical.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
from pandas.core.dtypes.missing import (
7474
is_valid_na_for_dtype,
7575
isna,
76-
notna,
7776
)
7877

7978
from pandas.core import (
@@ -1134,14 +1133,17 @@ def remove_categories(self, removals):
11341133
[NaN, 'c', 'b', 'c', NaN]
11351134
Categories (2, object): ['b', 'c']
11361135
"""
1136+
from pandas import Index
1137+
11371138
if not is_list_like(removals):
11381139
removals = [removals]
11391140

1140-
removals = {x for x in set(removals) if notna(x)}
1141+
removals = Index(removals).unique().dropna()
11411142
new_categories = self.dtype.categories.difference(removals)
11421143
not_included = removals.difference(self.dtype.categories)
11431144

11441145
if len(not_included) != 0:
1146+
not_included = set(not_included)
11451147
raise ValueError(f"removals must all be in old categories: {not_included}")
11461148

11471149
return self.set_categories(new_categories, ordered=self.ordered, rename=False)

0 commit comments

Comments
 (0)