Skip to content

API: fix str-accessor on CategoricalIndex #24005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ Categorical
- 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`).
- 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`)
- Bug when resampling :meth:`Dataframe.resample()` and aggregating on categorical data, the categorical dtype was getting lost. (:issue:`23227`)
- Bug in many methods of the ``.str``-accessor, which always failed on calling the ``CategoricalIndex.str`` constructor (:issue:`23555`, :issue:`23556`)

Datetimelike
^^^^^^^^^^^^
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pandas.core.dtypes.common import (
ensure_object, is_bool_dtype, is_categorical_dtype, is_integer,
is_list_like, is_object_dtype, is_re, is_scalar, is_string_like)
from pandas.core.dtypes.generic import ABCIndex, ABCSeries
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
from pandas.core.dtypes.missing import isna

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

if isinstance(arr, ABCIndex):
if isinstance(arr, ABCIndexClass):
arr = arr.to_series().reset_index(drop=True)

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

from pandas.core.index import Index, MultiIndex
from pandas import Index, Series, MultiIndex

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

if not hasattr(result, 'ndim') or not hasattr(result, 'dtype'):
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ def test_api_per_method(self, box, dtype,
and inferred_dtype in ['boolean', 'date', 'time']):
pytest.xfail(reason='Inferring incorrectly because of NaNs; '
'solved by GH 23167')
if box == Index and dtype == 'category':
pytest.xfail(reason='Broken methods on CategoricalIndex; '
'see GH 23556')

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

if inferred_dtype in allowed_types:
# xref GH 23555, GH 23556
method(*args, **kwargs) # works!
else:
# GH 23011, GH 23163
Expand Down