Skip to content

Commit 5d912fb

Browse files
jschendelPingviinituutti
authored andcommitted
BUG: Fix segfault in Categorical.set_categories (pandas-dev#24680)
1 parent e1ab095 commit 5d912fb

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@ Categorical
15011501
- Bug in :meth:`Series.where` losing the categorical dtype for categorical data (:issue:`24077`)
15021502
- Bug in :meth:`Categorical.apply` where ``NaN`` values could be handled unpredictably. They now remain unchanged (:issue:`24241`)
15031503
- Bug in :class:`Categorical` comparison methods incorrectly raising ``ValueError`` when operating against a :class:`DataFrame` (:issue:`24630`)
1504+
- Bug in :meth:`Categorical.set_categories` where setting fewer new categories with ``rename=True`` caused a segmentation fault (:issue:`24675`)
15041505

15051506
Datetimelike
15061507
^^^^^^^^^^^^

pandas/core/arrays/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ def set_categories(self, new_categories, ordered=None, rename=False,
852852
if (cat.dtype.categories is not None and
853853
len(new_dtype.categories) < len(cat.dtype.categories)):
854854
# remove all _codes which are larger and set to -1/NaN
855-
self._codes[self._codes >= len(new_dtype.categories)] = -1
855+
cat._codes[cat._codes >= len(new_dtype.categories)] = -1
856856
else:
857-
codes = _recode_for_categories(self.codes, self.categories,
857+
codes = _recode_for_categories(cat.codes, cat.categories,
858858
new_dtype.categories)
859859
cat._codes = codes
860860
cat._dtype = new_dtype

pandas/tests/arrays/categorical/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ def test_set_categories_many(self, values, categories, new_categories,
310310
result = c.set_categories(new_categories, ordered=ordered)
311311
tm.assert_categorical_equal(result, expected)
312312

313+
def test_set_categories_rename_less(self):
314+
# GH 24675
315+
cat = Categorical(['A', 'B'])
316+
result = cat.set_categories(['A'], rename=True)
317+
expected = Categorical(['A', np.nan])
318+
tm.assert_categorical_equal(result, expected)
319+
313320
def test_set_categories_private(self):
314321
cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
315322
cat._set_categories(['a', 'c', 'd', 'e'])

0 commit comments

Comments
 (0)