Skip to content

Commit b74e0a3

Browse files
committed
API: Implement Series.cat.codes and rename the proxy class
* Series should also expose Series.cat.codes as a Series of codes. See discussion in pandas-dev#8074 (comment) * Rename CategoricalProperties CategoricalAccessor. It has more than properties, so "accessor" is more appropiate.
1 parent 8fd654c commit b74e0a3

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

pandas/core/categorical.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ def describe(self):
11211121

11221122
##### The Series.cat accessor #####
11231123

1124-
class CategoricalProperties(PandasDelegate):
1124+
class CategoricalAccessor(PandasDelegate):
11251125
"""
11261126
Accessor object for categorical properties of the Series values.
11271127
@@ -1144,17 +1144,22 @@ def _delegate_property_get(self, name):
11441144
def _delegate_property_set(self, name, new_values):
11451145
return setattr(self.categorical, name, new_values)
11461146

1147+
@property
1148+
def codes(self):
1149+
from pandas import Series
1150+
return Series(self.categorical.codes, index=self.index)
1151+
11471152
def _delegate_method(self, name, *args, **kwargs):
11481153
from pandas import Series
11491154
method = getattr(self.categorical, name)
11501155
res = method(*args, **kwargs)
11511156
if not res is None:
11521157
return Series(res, index=self.index)
11531158

1154-
CategoricalProperties._add_delegate_accessors(delegate=Categorical,
1159+
CategoricalAccessor._add_delegate_accessors(delegate=Categorical,
11551160
accessors=["levels", "ordered"],
11561161
typ='property')
1157-
CategoricalProperties._add_delegate_accessors(delegate=Categorical,
1162+
CategoricalAccessor._add_delegate_accessors(delegate=Categorical,
11581163
accessors=["reorder_levels", "remove_unused_levels"],
11591164
typ='method')
11601165

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2442,10 +2442,10 @@ def dt(self):
24422442

24432443
@cache_readonly
24442444
def cat(self):
2445-
from pandas.core.categorical import CategoricalProperties
2445+
from pandas.core.categorical import CategoricalAccessor
24462446
if not com.is_categorical_dtype(self.dtype):
24472447
raise TypeError("Can only use .cat accessor with a 'category' dtype")
2448-
return CategoricalProperties(self.values, self.index)
2448+
return CategoricalAccessor(self.values, self.index)
24492449

24502450
Series._setup_axes(['index'], info_axis=0, stat_axis=0,
24512451
aliases={'rows': 0})

pandas/tests/test_categorical.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1051,14 +1051,18 @@ def test_series_delegations(self):
10511051
self.assertRaises(TypeError, lambda : Series(np.arange(5.)).cat)
10521052
self.assertRaises(TypeError, lambda : Series([Timestamp('20130101')]).cat)
10531053

1054-
# Series should delegate calls to '.level', '.ordered' and '.reorder()' to the categorical
1054+
# Series should delegate calls to '.level', '.codes', '.ordered' and the
1055+
# methods '.reorder_levels()' 'drop_unused_levels()' to the categorical
10551056
s = Series(Categorical(["a","b","c","a"], ordered=True))
10561057
exp_levels = np.array(["a","b","c"])
10571058
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
1058-
10591059
s.cat.levels = [1,2,3]
10601060
exp_levels = np.array([1,2,3])
10611061
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
1062+
1063+
exp_codes = Series(com._ensure_platform_int([0,1,2,0]))
1064+
tm.assert_series_equal(s.cat.codes, exp_codes)
1065+
10621066
self.assertEqual(s.cat.ordered, True)
10631067
s.cat.ordered = False
10641068
self.assertEqual(s.cat.ordered, False)
@@ -2087,7 +2091,7 @@ def test_numeric_like_ops(self):
20872091

20882092
def test_cat_tab_completition(self):
20892093
# test the tab completion display
2090-
ok_for_cat = ['levels','ordered','reorder_levels','remove_unused_levels']
2094+
ok_for_cat = ['levels','codes','ordered','reorder_levels','remove_unused_levels']
20912095
def get_dir(s):
20922096
results = [ r for r in s.cat.__dir__() if not r.startswith('_') ]
20932097
return list(sorted(set(results)))

0 commit comments

Comments
 (0)