Skip to content

Commit 141e509

Browse files
committed
Fixup set_categories inplace test
1 parent 41172ce commit 141e509

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pandas/core/categorical.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,24 @@ def _get_labels(self):
601601
labels = property(fget=_get_labels, fset=_set_codes)
602602

603603
def _set_categories(self, categories, fastpath=False):
604-
""" Sets new categories
604+
""" Sets new categories inplace
605605
606606
Parameters
607607
----------
608608
fastpath : boolean (default: False)
609609
Don't perform validation of the categories for uniqueness or nulls
610610
611+
Examples
612+
--------
613+
>>> c = Categorical(['a', 'b'])
614+
>>> c
615+
[a, b]
616+
Categories (2, object): [a, b]
617+
618+
>>> c._set_categories(pd.Index(['a', 'c']))
619+
>>> c
620+
[a, c]
621+
Categories (2, object): [a, c]
611622
"""
612623

613624
if fastpath:

pandas/tests/test_categorical.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,15 @@ def test_set_dtype_nans(self):
965965

966966
def test_set_categories_private(self):
967967
cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
968-
result = cat._set_categories(['a', 'b', 'c', 'd', 'e'])
969-
expected = Categorical(['a', 'b', 'c'], categories=list('abcde'))
970-
tm.assert_categorical_equal(result, expected)
968+
cat._set_categories(['a', 'c', 'd', 'e'])
969+
expected = Categorical(['a', 'c', 'd'], categories=list('acde'))
970+
tm.assert_categorical_equal(cat, expected)
971971

972972
# fastpath
973-
result = cat._set_categories(['a', 'b', 'c', 'd', 'e'], fastpath=True)
974-
tm.assert_categorical_equal(result, expected)
973+
cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
974+
cat._set_categories(['a', 'c', 'd', 'e'], fastpath=True)
975+
expected = Categorical(['a', 'c', 'd'], categories=list('acde'))
976+
tm.assert_categorical_equal(cat, expected)
975977

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

0 commit comments

Comments
 (0)