Skip to content

BUG: Fix segfault in Categorical.set_categories #24680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member Author

@jschendel jschendel Jan 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change shouldn't alter existing behavior, as self and cat should be identical within this branch of the if/else. More so a defensive future-proofing change in case future modifications cause a divergence between self and cat prior to this. All operations at this point should be in relation to cat anyways, as that's the object we'll be returning.

new_dtype.categories)
cat._codes = codes
cat._dtype = new_dtype
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down