Skip to content

Commit dc3e92a

Browse files
h-vetinariPingviinituutti
authored andcommitted
API: fix str-accessor on CategoricalIndex (pandas-dev#24005)
1 parent 94bbed4 commit dc3e92a

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ Categorical
12561256
- Bug in :meth:`Categorical.take` with a user-provided ``fill_value`` not encoding the ``fill_value``, which could result in a ``ValueError``, incorrect results, or a segmentation fault (:issue:`23296`).
12571257
- In meth:`Series.unstack`, specifying a ``fill_value`` not present in the categories now raises a ``TypeError`` rather than ignoring the ``fill_value`` (:issue:`23284`)
12581258
- Bug when resampling :meth:`Dataframe.resample()` and aggregating on categorical data, the categorical dtype was getting lost. (:issue:`23227`)
1259+
- Bug in many methods of the ``.str``-accessor, which always failed on calling the ``CategoricalIndex.str`` constructor (:issue:`23555`, :issue:`23556`)
12591260

12601261
Datetimelike
12611262
^^^^^^^^^^^^

pandas/core/strings.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pandas.core.dtypes.common import (
1616
ensure_object, is_bool_dtype, is_categorical_dtype, is_integer,
1717
is_list_like, is_object_dtype, is_re, is_scalar, is_string_like)
18-
from pandas.core.dtypes.generic import ABCIndex, ABCSeries
18+
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
1919
from pandas.core.dtypes.missing import isna
2020

2121
from pandas.core.algorithms import take_1d
@@ -931,7 +931,7 @@ def str_extractall(arr, pat, flags=0):
931931
if regex.groups == 0:
932932
raise ValueError("pattern contains no capture groups")
933933

934-
if isinstance(arr, ABCIndex):
934+
if isinstance(arr, ABCIndexClass):
935935
arr = arr.to_series().reset_index(drop=True)
936936

937937
names = dict(zip(regex.groupindex.values(), regex.groupindex.keys()))
@@ -1854,15 +1854,16 @@ def __iter__(self):
18541854
def _wrap_result(self, result, use_codes=True,
18551855
name=None, expand=None, fill_value=np.nan):
18561856

1857-
from pandas.core.index import Index, MultiIndex
1857+
from pandas import Index, Series, MultiIndex
18581858

18591859
# for category, we do the stuff on the categories, so blow it up
18601860
# to the full series again
18611861
# But for some operations, we have to do the stuff on the full values,
18621862
# so make it possible to skip this step as the method already did this
18631863
# before the transformation...
18641864
if use_codes and self._is_categorical:
1865-
result = take_1d(result, self._orig.cat.codes,
1865+
# if self._orig is a CategoricalIndex, there is no .cat-accessor
1866+
result = take_1d(result, Series(self._orig, copy=False).cat.codes,
18661867
fill_value=fill_value)
18671868

18681869
if not hasattr(result, 'ndim') or not hasattr(result, 'dtype'):

pandas/tests/test_strings.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ def test_api_per_method(self, box, dtype,
245245
and inferred_dtype in ['boolean', 'date', 'time']):
246246
pytest.xfail(reason='Inferring incorrectly because of NaNs; '
247247
'solved by GH 23167')
248-
if box == Index and dtype == 'category':
249-
pytest.xfail(reason='Broken methods on CategoricalIndex; '
250-
'see GH 23556')
251248

252249
t = box(values, dtype=dtype) # explicit dtype to avoid casting
253250
method = getattr(t.str, method_name)
@@ -264,6 +261,7 @@ def test_api_per_method(self, box, dtype,
264261
+ ['mixed', 'mixed-integer'] * mixed_allowed)
265262

266263
if inferred_dtype in allowed_types:
264+
# xref GH 23555, GH 23556
267265
method(*args, **kwargs) # works!
268266
else:
269267
# GH 23011, GH 23163

0 commit comments

Comments
 (0)