From d5ec2e7be496fe4a22b3873957ca7d17ca4024e9 Mon Sep 17 00:00:00 2001 From: jschendel Date: Tue, 8 Jan 2019 19:11:23 -0700 Subject: [PATCH] BUG: Fix segfault in Categorical.set_categories --- doc/source/whatsnew/v0.24.0.rst | 1 + pandas/core/arrays/categorical.py | 4 ++-- pandas/tests/arrays/categorical/test_api.py | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 21712b1bf5b6c..8d635e2f9fba3 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1501,6 +1501,7 @@ Categorical - Bug in :meth:`Series.where` losing the categorical dtype for categorical data (:issue:`24077`) - Bug in :meth:`Categorical.apply` where ``NaN`` values could be handled unpredictably. They now remain unchanged (:issue:`24241`) - Bug in :class:`Categorical` comparison methods incorrectly raising ``ValueError`` when operating against a :class:`DataFrame` (:issue:`24630`) +- Bug in :meth:`Categorical.set_categories` where setting fewer new categories with ``rename=True`` caused a segmentation fault (:issue:`24675`) Datetimelike ^^^^^^^^^^^^ diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index f88249d0fa6b2..1368232470402 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -852,9 +852,9 @@ def set_categories(self, new_categories, ordered=None, rename=False, if (cat.dtype.categories is not None and len(new_dtype.categories) < len(cat.dtype.categories)): # remove all _codes which are larger and set to -1/NaN - self._codes[self._codes >= len(new_dtype.categories)] = -1 + cat._codes[cat._codes >= len(new_dtype.categories)] = -1 else: - codes = _recode_for_categories(self.codes, self.categories, + codes = _recode_for_categories(cat.codes, cat.categories, new_dtype.categories) cat._codes = codes cat._dtype = new_dtype diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index 348bb947efef7..86dbc5ebf9fe1 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -310,6 +310,13 @@ def test_set_categories_many(self, values, categories, new_categories, result = c.set_categories(new_categories, ordered=ordered) tm.assert_categorical_equal(result, expected) + def test_set_categories_rename_less(self): + # GH 24675 + cat = Categorical(['A', 'B']) + result = cat.set_categories(['A'], rename=True) + expected = Categorical(['A', np.nan]) + tm.assert_categorical_equal(result, expected) + def test_set_categories_private(self): cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd']) cat._set_categories(['a', 'c', 'd', 'e'])