Skip to content

Categorical type #16015

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 7 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixup set_categories inplace test
  • Loading branch information
TomAugspurger committed Sep 23, 2017
commit 141e5094e13fecd68f1fc124e06e0349c29adc2a
13 changes: 12 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,24 @@ def _get_labels(self):
labels = property(fget=_get_labels, fset=_set_codes)

def _set_categories(self, categories, fastpath=False):
""" Sets new categories
""" Sets new categories inplace

Parameters
----------
fastpath : boolean (default: False)
Don't perform validation of the categories for uniqueness or nulls

Examples
--------
>>> c = Categorical(['a', 'b'])
>>> c
[a, b]
Categories (2, object): [a, b]

>>> c._set_categories(pd.Index(['a', 'c']))
>>> c
[a, c]
Categories (2, object): [a, c]
"""

if fastpath:
Copy link
Contributor

Choose a reason for hiding this comment

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

lets deprecate fastpath (and convert fastpath -> dtype construction, and instead allow dtype to be passed

Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,13 +965,15 @@ def test_set_dtype_nans(self):

def test_set_categories_private(self):
cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
result = cat._set_categories(['a', 'b', 'c', 'd', 'e'])
expected = Categorical(['a', 'b', 'c'], categories=list('abcde'))
tm.assert_categorical_equal(result, expected)
cat._set_categories(['a', 'c', 'd', 'e'])
expected = Categorical(['a', 'c', 'd'], categories=list('acde'))
tm.assert_categorical_equal(cat, expected)

# fastpath
result = cat._set_categories(['a', 'b', 'c', 'd', 'e'], fastpath=True)
tm.assert_categorical_equal(result, expected)
cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
cat._set_categories(['a', 'c', 'd', 'e'], fastpath=True)
expected = Categorical(['a', 'c', 'd'], categories=list('acde'))
tm.assert_categorical_equal(cat, expected)

@pytest.mark.parametrize('values, categories, new_categories', [
# No NaNs, same cats, same order
Expand Down