Skip to content

Commit 9cc3333

Browse files
Giftlinjorisvandenbossche
authored andcommitted
BUG: preserve name in set_categories (#17509) (#17517)
1 parent cbb090f commit 9cc3333

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

doc/source/categorical.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Using ``.describe()`` on categorical data will produce similar output to a `Seri
146146
df.describe()
147147
df["cat"].describe()
148148
149+
.. _categorical.cat:
150+
149151
Working with categories
150152
-----------------------
151153

doc/source/whatsnew/v0.21.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ Categorical
571571
- Bug in the categorical constructor with empty values and categories causing
572572
the ``.categories`` to be an empty ``Float64Index`` rather than an empty
573573
``Index`` with object dtype (:issue:`17248`)
574+
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>' not preserving the original Series' name (:issue:`17509`)
574575

575576
PyPy
576577
^^^^

pandas/core/categorical.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,9 +2054,10 @@ class CategoricalAccessor(PandasDelegate, NoNewAttributesMixin):
20542054
20552055
"""
20562056

2057-
def __init__(self, values, index):
2057+
def __init__(self, values, index, name):
20582058
self.categorical = values
20592059
self.index = index
2060+
self.name = name
20602061
self._freeze()
20612062

20622063
def _delegate_property_get(self, name):
@@ -2075,14 +2076,15 @@ def _delegate_method(self, name, *args, **kwargs):
20752076
method = getattr(self.categorical, name)
20762077
res = method(*args, **kwargs)
20772078
if res is not None:
2078-
return Series(res, index=self.index)
2079+
return Series(res, index=self.index, name=self.name)
20792080

20802081
@classmethod
20812082
def _make_accessor(cls, data):
20822083
if not is_categorical_dtype(data.dtype):
20832084
raise AttributeError("Can only use .cat accessor with a "
20842085
"'category' dtype")
2085-
return CategoricalAccessor(data.values, data.index)
2086+
return CategoricalAccessor(data.values, data.index,
2087+
getattr(data, 'name', None),)
20862088

20872089

20882090
CategoricalAccessor._add_delegate_accessors(delegate=Categorical,

pandas/tests/test_categorical.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ def test_getitem_listlike(self):
5757
expected = c[np.array([100000]).astype(np.int64)].codes
5858
tm.assert_numpy_array_equal(result, expected)
5959

60+
@pytest.mark.parametrize(
61+
"method",
62+
[
63+
lambda x: x.cat.set_categories([1, 2, 3]),
64+
lambda x: x.cat.reorder_categories([2, 3, 1], ordered=True),
65+
lambda x: x.cat.rename_categories([1, 2, 3]),
66+
lambda x: x.cat.remove_unused_categories(),
67+
lambda x: x.cat.remove_categories([2]),
68+
lambda x: x.cat.add_categories([4]),
69+
lambda x: x.cat.as_ordered(),
70+
lambda x: x.cat.as_unordered(),
71+
])
72+
def test_getname_categorical_accessor(self, method):
73+
# GH 17509
74+
s = pd.Series([1, 2, 3], name='A').astype('category')
75+
expected = 'A'
76+
result = method(s).name
77+
assert result == expected
78+
6079
def test_getitem_category_type(self):
6180
# GH 14580
6281
# test iloc() on Series with Categorical data

0 commit comments

Comments
 (0)