Skip to content

Call finalize in Series.str #37015

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 3 commits into from
Oct 10, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ Other

- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly raising ``AssertionError`` instead of ``ValueError`` when invalid parameter combinations are passed (:issue:`36045`)
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` with numeric values and string ``to_replace`` (:issue:`34789`)
- Fixed metadata propagation in the :class:`Series.dt` accessor (:issue:`28283`)
- Fixed metadata propagation in the :class:`Series.dt` and :class:`Series.str` accessors (:issue:`28283`)
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)

.. ---------------------------------------------------------------------------
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ def _wrap_result(
from pandas import Index, MultiIndex

if not hasattr(result, "ndim") or not hasattr(result, "dtype"):
if isinstance(result, ABCDataFrame):
result = result.__finalize__(self._orig, name="str")
return result
assert result.ndim < 3

Expand Down Expand Up @@ -324,6 +326,11 @@ def cons_row(x):
# Must be a Series
cons = self._orig._constructor
result = cons(result, name=name, index=index)
result = result.__finalize__(self._orig, method="str")
if name is not None and result.ndim == 1:
# __finalize__ might copy over the original name, but we may
# want the new name (e.g. str.extract).
result.name = name
return result

def _get_series_list(self, others):
Expand Down Expand Up @@ -597,6 +604,7 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
else:
dtype = self._orig.dtype
result = Series(result, dtype=dtype, index=data.index, name=self._orig.name)
result = result.__finalize__(self._orig, method="str_cat")
return result

_shared_docs[
Expand Down Expand Up @@ -3034,7 +3042,8 @@ def str_extract(arr, pat, flags=0, expand=True):
if not isinstance(expand, bool):
raise ValueError("expand must be True or False")
if expand:
return _str_extract_frame(arr._orig, pat, flags=flags)
result = _str_extract_frame(arr._orig, pat, flags=flags)
return result.__finalize__(arr._orig, method="str_extract")
else:
result, name = _str_extract_noexpand(arr._orig, pat, flags=flags)
return arr._wrap_result(result, name=name, expand=expand)
Expand Down
16 changes: 3 additions & 13 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,22 +598,13 @@ def test_binops(args, annotate, all_arithmetic_functions):
[
operator.methodcaller("capitalize"),
operator.methodcaller("casefold"),
pytest.param(
operator.methodcaller("cat", ["a"]),
marks=pytest.mark.xfail(reason="finalize not called."),
),
operator.methodcaller("cat", ["a"]),
operator.methodcaller("contains", "a"),
operator.methodcaller("count", "a"),
operator.methodcaller("encode", "utf-8"),
operator.methodcaller("endswith", "a"),
pytest.param(
operator.methodcaller("extract", r"(\w)(\d)"),
marks=pytest.mark.xfail(reason="finalize not called."),
),
pytest.param(
operator.methodcaller("extract", r"(\w)(\d)"),
marks=pytest.mark.xfail(reason="finalize not called."),
),
operator.methodcaller("extract", r"(\w)(\d)"),
operator.methodcaller("extract", r"(\w)(\d)", expand=False),
operator.methodcaller("find", "a"),
operator.methodcaller("findall", "a"),
operator.methodcaller("get", 0),
Expand Down Expand Up @@ -655,7 +646,6 @@ def test_binops(args, annotate, all_arithmetic_functions):
],
ids=idfn,
)
@not_implemented_mark
def test_string_method(method):
s = pd.Series(["a1"])
s.attrs = {"a": 1}
Expand Down