Skip to content

Commit 58c2150

Browse files
Call finalize in Series.str (#37015)
1 parent e2788da commit 58c2150

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ Other
475475

476476
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly raising ``AssertionError`` instead of ``ValueError`` when invalid parameter combinations are passed (:issue:`36045`)
477477
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` with numeric values and string ``to_replace`` (:issue:`34789`)
478-
- Fixed metadata propagation in the :class:`Series.dt` accessor (:issue:`28283`)
478+
- Fixed metadata propagation in the :class:`Series.dt` and :class:`Series.str` accessors (:issue:`28283`)
479479
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
480480
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError``, from a bare ``Exception`` previously (:issue:`35744`)
481481

pandas/core/strings/accessor.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ def _wrap_result(
247247
from pandas import Index, MultiIndex
248248

249249
if not hasattr(result, "ndim") or not hasattr(result, "dtype"):
250+
if isinstance(result, ABCDataFrame):
251+
result = result.__finalize__(self._orig, name="str")
250252
return result
251253
assert result.ndim < 3
252254

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

329336
def _get_series_list(self, others):
@@ -597,6 +604,7 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
597604
else:
598605
dtype = self._orig.dtype
599606
result = Series(result, dtype=dtype, index=data.index, name=self._orig.name)
607+
result = result.__finalize__(self._orig, method="str_cat")
600608
return result
601609

602610
_shared_docs[
@@ -3034,7 +3042,8 @@ def str_extract(arr, pat, flags=0, expand=True):
30343042
if not isinstance(expand, bool):
30353043
raise ValueError("expand must be True or False")
30363044
if expand:
3037-
return _str_extract_frame(arr._orig, pat, flags=flags)
3045+
result = _str_extract_frame(arr._orig, pat, flags=flags)
3046+
return result.__finalize__(arr._orig, method="str_extract")
30383047
else:
30393048
result, name = _str_extract_noexpand(arr._orig, pat, flags=flags)
30403049
return arr._wrap_result(result, name=name, expand=expand)

pandas/tests/generic/test_finalize.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,13 @@ def test_binops(args, annotate, all_arithmetic_functions):
598598
[
599599
operator.methodcaller("capitalize"),
600600
operator.methodcaller("casefold"),
601-
pytest.param(
602-
operator.methodcaller("cat", ["a"]),
603-
marks=pytest.mark.xfail(reason="finalize not called."),
604-
),
601+
operator.methodcaller("cat", ["a"]),
605602
operator.methodcaller("contains", "a"),
606603
operator.methodcaller("count", "a"),
607604
operator.methodcaller("encode", "utf-8"),
608605
operator.methodcaller("endswith", "a"),
609-
pytest.param(
610-
operator.methodcaller("extract", r"(\w)(\d)"),
611-
marks=pytest.mark.xfail(reason="finalize not called."),
612-
),
613-
pytest.param(
614-
operator.methodcaller("extract", r"(\w)(\d)"),
615-
marks=pytest.mark.xfail(reason="finalize not called."),
616-
),
606+
operator.methodcaller("extract", r"(\w)(\d)"),
607+
operator.methodcaller("extract", r"(\w)(\d)", expand=False),
617608
operator.methodcaller("find", "a"),
618609
operator.methodcaller("findall", "a"),
619610
operator.methodcaller("get", 0),
@@ -655,7 +646,6 @@ def test_binops(args, annotate, all_arithmetic_functions):
655646
],
656647
ids=idfn,
657648
)
658-
@not_implemented_mark
659649
def test_string_method(method):
660650
s = pd.Series(["a1"])
661651
s.attrs = {"a": 1}

0 commit comments

Comments
 (0)