Skip to content

Commit 6d06906

Browse files
benjaminrPingviinituutti
authored andcommitted
DEPR: CategoricalAccessor.categorical removed in 0.24.0rc1 pandas-dev#24751 (pandas-dev#24754)
* Implementation for CategoricalAccessor.categorical removed in 0.24.0rc1 pandas-dev#24751
1 parent c584256 commit 6d06906

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,7 @@ Deprecations
13091309
- :meth:`Series.clip_lower`, :meth:`Series.clip_upper`, :meth:`DataFrame.clip_lower` and :meth:`DataFrame.clip_upper` are deprecated and will be removed in a future version. Use ``Series.clip(lower=threshold)``, ``Series.clip(upper=threshold)`` and the equivalent ``DataFrame`` methods (:issue:`24203`)
13101310
- :meth:`Series.nonzero` is deprecated and will be removed in a future version (:issue:`18262`)
13111311
- Passing an integer to :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtypes is deprecated, will raise ``TypeError`` in a future version. Use ``obj.fillna(pd.Timedelta(...))`` instead (:issue:`24694`)
1312+
- ``Series.cat.categorical``, ``Series.cat.name`` and ``Sersies.cat.index`` have been deprecated. Use the attributes on ``Series.cat`` or ``Series`` directly. (:issue:`24751`).
13121313

13131314
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
13141315

pandas/core/arrays/categorical.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -2498,8 +2498,8 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):
24982498
def __init__(self, data):
24992499
self._validate(data)
25002500
self._parent = data.values
2501-
self.index = data.index
2502-
self.name = data.name
2501+
self._index = data.index
2502+
self._name = data.name
25032503
self._freeze()
25042504

25052505
@staticmethod
@@ -2520,15 +2520,47 @@ def codes(self):
25202520
Return Series of codes as well as the index.
25212521
"""
25222522
from pandas import Series
2523-
return Series(self._parent.codes, index=self.index)
2523+
return Series(self._parent.codes, index=self._index)
25242524

25252525
def _delegate_method(self, name, *args, **kwargs):
25262526
from pandas import Series
25272527
method = getattr(self._parent, name)
25282528
res = method(*args, **kwargs)
25292529
if res is not None:
2530-
return Series(res, index=self.index, name=self.name)
2530+
return Series(res, index=self._index, name=self._name)
25312531

2532+
@property
2533+
def categorical(self):
2534+
# Note: Upon deprecation, `test_tab_completion_with_categorical` will
2535+
# need to be updated. `categorical` will need to be removed from
2536+
# `ok_for_cat`.
2537+
warn("`Series.cat.categorical` has been deprecated. Use the "
2538+
"attributes on 'Series.cat' directly instead.",
2539+
FutureWarning,
2540+
stacklevel=2)
2541+
return self._parent
2542+
2543+
@property
2544+
def name(self):
2545+
# Note: Upon deprecation, `test_tab_completion_with_categorical` will
2546+
# need to be updated. `name` will need to be removed from
2547+
# `ok_for_cat`.
2548+
warn("`Series.cat.name` has been deprecated. Use `Series.name` "
2549+
"instead.",
2550+
FutureWarning,
2551+
stacklevel=2)
2552+
return self._name
2553+
2554+
@property
2555+
def index(self):
2556+
# Note: Upon deprecation, `test_tab_completion_with_categorical` will
2557+
# need to be updated. `index` will need to be removed from
2558+
# ok_for_cat`.
2559+
warn("`Series.cat.index` has been deprecated. Use `Series.index` "
2560+
"instead.",
2561+
FutureWarning,
2562+
stacklevel=2)
2563+
return self._index
25322564

25332565
# utility routines
25342566

pandas/tests/arrays/categorical/test_warnings.py

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
import pandas as pd
56
import pandas.util.testing as tm
67

78

@@ -16,3 +17,15 @@ def test_tab_complete_warning(self, ip):
1617
with tm.assert_produces_warning(None):
1718
with provisionalcompleter('ignore'):
1819
list(ip.Completer.completions('c.', 1))
20+
21+
def test_CategoricalAccessor_categorical_deprecation(object):
22+
with tm.assert_produces_warning(FutureWarning):
23+
pd.Series(['a', 'b'], dtype='category').cat.categorical
24+
25+
def test_CategoricalAccessor_name_deprecation(object):
26+
with tm.assert_produces_warning(FutureWarning):
27+
pd.Series(['a', 'b'], dtype='category').cat.name
28+
29+
def test_CategoricalAccessor_index_deprecation(object):
30+
with tm.assert_produces_warning(FutureWarning):
31+
pd.Series(['a', 'b'], dtype='category').cat.index

pandas/tests/series/test_api.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,11 @@ def test_tab_completion(self):
245245

246246
def test_tab_completion_with_categorical(self):
247247
# test the tab completion display
248-
ok_for_cat = ['categories', 'codes', 'ordered', 'set_categories',
249-
'add_categories', 'remove_categories',
250-
'rename_categories', 'reorder_categories',
251-
'remove_unused_categories', 'as_ordered', 'as_unordered']
248+
ok_for_cat = ['name', 'index', 'categorical', 'categories', 'codes',
249+
'ordered', 'set_categories', 'add_categories',
250+
'remove_categories', 'rename_categories',
251+
'reorder_categories', 'remove_unused_categories',
252+
'as_ordered', 'as_unordered']
252253

253254
def get_dir(s):
254255
results = [r for r in s.cat.__dir__() if not r.startswith('_')]

pandas/tests/test_config.py

-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def test_deprecate_option(self):
251251
KeyError,
252252
message="Nonexistent option didn't raise KeyError"):
253253
self.cf.get_option('foo')
254-
255254
assert len(w) == 1 # should have raised one warning
256255
assert 'deprecated' in str(w[-1]) # we get the default message
257256

0 commit comments

Comments
 (0)