Skip to content

Commit 971f62b

Browse files
committed
API: Call finalize in more Series methods [WIP]
Progress towards pandas-dev#28283. This calls `finalize` for all the public series methods where I think it makes sense.
1 parent 2a834cf commit 971f62b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,7 @@ def align(
39563956
fill_axis=fill_axis,
39573957
broadcast_axis=broadcast_axis,
39583958
)
3959+
# TODO(API): Should these be method=align-left, and align-right?
39593960
a.__finalize__((self, other), method="align")
39603961
b.__finalize__((self, other), method="align")
39613962
return a, b

pandas/core/strings.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ def _str_extract_frame(arr, pat, flags=0):
951951
str_extract(expand=True), and always returns a DataFrame.
952952
953953
"""
954+
# TODO: why doesn't this go through wrap_result?
954955
from pandas import DataFrame
955956

956957
regex = re.compile(pat, flags=flags)
@@ -2253,11 +2254,15 @@ def cons_row(x):
22532254
index = self._orig.index
22542255
if expand:
22552256
cons = self._orig._constructor_expanddim
2256-
result = cons(result, columns=name, index=index, dtype=dtype)
2257+
result = cons(
2258+
result, columns=name, index=index, dtype=dtype
2259+
).__finalize__(self._orig, method="str")
22572260
else:
22582261
# Must be a Series
22592262
cons = self._orig._constructor
2260-
result = cons(result, name=name, index=index, dtype=dtype)
2263+
result = cons(result, name=name, index=index, dtype=dtype).__finalize__(
2264+
self._orig, method="str"
2265+
)
22612266
return result
22622267

22632268
def _get_series_list(self, others):

pandas/tests/generic/test_finalize.py

+49
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@
2323
(pd.Series, ([0, 0],), operator.methodcaller("drop_duplicates")),
2424
(pd.Series, ([0, 0],), operator.methodcaller("duplicated")),
2525
(pd.Series, ([0, 0],), operator.methodcaller("round")),
26+
(pd.Series, ([0, 0],), operator.methodcaller("rename", lambda x: x + 1)),
27+
(pd.Series, ([0, 0],), operator.methodcaller("rename", "name")),
28+
(pd.Series, ([0, 0],), operator.methodcaller("set_axis", ["a", "b"])),
29+
(pd.Series, ([0, 0],), operator.methodcaller("reindex", [1, 0])),
30+
(pd.Series, ([0, 0],), operator.methodcaller("drop", [0])),
31+
(pd.Series, (pd.array([0, pd.NA]),), operator.methodcaller("fillna", 0)),
32+
(pd.Series, ([0, 0],), operator.methodcaller("replace", {0: 1})),
33+
(pd.Series, ([0, 0],), operator.methodcaller("shift")),
34+
(pd.Series, ([0, 0],), operator.methodcaller("isin", [0, 1])),
35+
(pd.Series, ([0, 0],), operator.methodcaller("between", 0, 2)),
36+
(pd.Series, ([0, 0],), operator.methodcaller("isna")),
37+
(pd.Series, ([0, 0],), operator.methodcaller("isnull")),
38+
(pd.Series, ([0, 0],), operator.methodcaller("notna")),
39+
(pd.Series, ([0, 0],), operator.methodcaller("notnull")),
40+
(
41+
pd.Series,
42+
([0], pd.period_range("2000", periods=1)),
43+
operator.methodcaller("to_timestamp"),
44+
),
45+
(
46+
pd.Series,
47+
([0], pd.date_range("2000", periods=1)),
48+
operator.methodcaller("to_period"),
49+
),
2650
]
2751

2852

@@ -34,6 +58,31 @@ def ndframe_method(request):
3458
return request.param
3559

3660

61+
@pytest.mark.parametrize(
62+
"method",
63+
[
64+
operator.methodcaller("upper"),
65+
pytest.param(
66+
operator.methodcaller("extract", r"(\w)(\d)"),
67+
marks=pytest.mark.xfail(reason="finalize not called."),
68+
),
69+
],
70+
)
71+
def test_string_method(method):
72+
s = pd.Series(["a1"])
73+
s.attrs = {"a": 1}
74+
result = method(s.str)
75+
assert result.attrs == {"a": 1}
76+
77+
78+
@pytest.mark.xfail(reason="TODO")
79+
def test_datetime_method():
80+
s = pd.Series(pd.date_range("2000", periods=4))
81+
s.attrs = {"a": 1}
82+
result = s.dt.date
83+
assert result.attrs == {"a": 1}
84+
85+
3786
def test_finalize_called(ndframe_method):
3887
cls, init_args, method = ndframe_method
3988
ndframe = cls(*init_args)

0 commit comments

Comments
 (0)