Skip to content

Commit e2c2a7b

Browse files
jbrockmendelyehoshuadimarsky
authored andcommitted
BUG: fix __finalize__ xfails (pandas-dev#45721)
1 parent 4db6f24 commit e2c2a7b

File tree

5 files changed

+23
-38
lines changed

5 files changed

+23
-38
lines changed

pandas/core/frame.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -9498,7 +9498,7 @@ def _series_round(ser: Series, decimals: int):
94989498
if len(new_cols) > 0:
94999499
return self._constructor(
95009500
concat(new_cols, axis=1), index=self.index, columns=self.columns
9501-
)
9501+
).__finalize__(self, method="round")
95029502
else:
95039503
return self
95049504

@@ -9886,7 +9886,8 @@ def count(
98869886
FutureWarning,
98879887
stacklevel=find_stack_level(),
98889888
)
9889-
return self._count_level(level, axis=axis, numeric_only=numeric_only)
9889+
res = self._count_level(level, axis=axis, numeric_only=numeric_only)
9890+
return res.__finalize__(self, method="count")
98909891

98919892
if numeric_only:
98929893
frame = self._get_numeric_data()
@@ -9909,7 +9910,7 @@ def count(
99099910
counts, index=frame._get_agg_axis(axis)
99109911
)
99119912

9912-
return result.astype("int64")
9913+
return result.astype("int64").__finalize__(self, method="count")
99139914

99149915
def _count_level(self, level: Level, axis: int = 0, numeric_only: bool = False):
99159916
if numeric_only:
@@ -10539,13 +10540,14 @@ def quantile(
1053910540
dtype = cdtype
1054010541

1054110542
if is_list_like(q):
10542-
return self._constructor([], index=q, columns=cols, dtype=dtype)
10543+
res = self._constructor([], index=q, columns=cols, dtype=dtype)
10544+
return res.__finalize__(self, method="quantile")
1054310545
return self._constructor_sliced([], index=cols, name=q, dtype=dtype)
1054410546

1054510547
res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation)
1054610548

1054710549
result = self._constructor(res)
10548-
return result
10550+
return result.__finalize__(self, method="quantile")
1054910551

1055010552
@doc(NDFrame.asfreq, **_shared_doc_kwargs)
1055110553
def asfreq(
@@ -10762,7 +10764,7 @@ def isin(self, values) -> DataFrame:
1076210764
from pandas.core.reshape.concat import concat
1076310765

1076410766
values = collections.defaultdict(list, values)
10765-
return concat(
10767+
result = concat(
1076610768
(
1076710769
self.iloc[:, [i]].isin(values[col])
1076810770
for i, col in enumerate(self.columns)
@@ -10772,23 +10774,24 @@ def isin(self, values) -> DataFrame:
1077210774
elif isinstance(values, Series):
1077310775
if not values.index.is_unique:
1077410776
raise ValueError("cannot compute isin with a duplicate axis.")
10775-
return self.eq(values.reindex_like(self), axis="index")
10777+
result = self.eq(values.reindex_like(self), axis="index")
1077610778
elif isinstance(values, DataFrame):
1077710779
if not (values.columns.is_unique and values.index.is_unique):
1077810780
raise ValueError("cannot compute isin with a duplicate axis.")
10779-
return self.eq(values.reindex_like(self))
10781+
result = self.eq(values.reindex_like(self))
1078010782
else:
1078110783
if not is_list_like(values):
1078210784
raise TypeError(
1078310785
"only list-like or dict-like objects are allowed "
1078410786
"to be passed to DataFrame.isin(), "
1078510787
f"you passed a '{type(values).__name__}'"
1078610788
)
10787-
return self._constructor(
10789+
result = self._constructor(
1078810790
algorithms.isin(self.values.ravel(), values).reshape(self.shape),
1078910791
self.index,
1079010792
self.columns,
1079110793
)
10794+
return result.__finalize__(self, method="isin")
1079210795

1079310796
# ----------------------------------------------------------------------
1079410797
# Add index and columns

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ def __abs__(self: NDFrameT) -> NDFrameT:
16461646

16471647
@final
16481648
def __round__(self: NDFrameT, decimals: int = 0) -> NDFrameT:
1649-
return self.round(decimals)
1649+
return self.round(decimals).__finalize__(self, method="__round__")
16501650

16511651
# -------------------------------------------------------------------------
16521652
# Label or Level Combination Helpers
@@ -10383,7 +10383,7 @@ def pct_change(
1038310383
# We want to restore the original index
1038410384
rs = rs.loc[~rs.index.duplicated()]
1038510385
rs = rs.reindex_like(data)
10386-
return rs
10386+
return rs.__finalize__(self, method="pct_change")
1038710387

1038810388
@final
1038910389
def _agg_by_level(

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,8 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame:
18121812
columns = Index([name])
18131813

18141814
mgr = self._mgr.to_2d_mgr(columns)
1815-
return self._constructor_expanddim(mgr)
1815+
df = self._constructor_expanddim(mgr)
1816+
return df.__finalize__(self, method="to_frame")
18161817

18171818
def _set_name(self, name, inplace=False) -> Series:
18181819
"""

pandas/tests/generic/test_duplicate_labels.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ def test_preserved_frame(self):
6363
assert df.loc[["a"]].flags.allows_duplicate_labels is False
6464
assert df.loc[:, ["A", "B"]].flags.allows_duplicate_labels is False
6565

66-
@not_implemented
6766
def test_to_frame(self):
68-
s = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False)
69-
assert s.to_frame().flags.allows_duplicate_labels is False
67+
ser = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False)
68+
assert ser.to_frame().flags.allows_duplicate_labels is False
7069

7170
@pytest.mark.parametrize("func", ["add", "sub"])
7271
@pytest.mark.parametrize(

pandas/tests/generic/test_finalize.py

+5-23
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@
3838
(pd.Series, ([0],), operator.methodcaller("take", [])),
3939
(pd.Series, ([0],), operator.methodcaller("__getitem__", [True])),
4040
(pd.Series, ([0],), operator.methodcaller("repeat", 2)),
41-
pytest.param(
42-
(pd.Series, ([0],), operator.methodcaller("reset_index")),
43-
marks=pytest.mark.xfail,
44-
),
41+
(pd.Series, ([0],), operator.methodcaller("reset_index")),
4542
(pd.Series, ([0],), operator.methodcaller("reset_index", drop=True)),
46-
pytest.param(
47-
(pd.Series, ([0],), operator.methodcaller("to_frame")), marks=pytest.mark.xfail
48-
),
43+
(pd.Series, ([0],), operator.methodcaller("to_frame")),
4944
(pd.Series, ([0, 0],), operator.methodcaller("drop_duplicates")),
5045
(pd.Series, ([0, 0],), operator.methodcaller("duplicated")),
5146
(pd.Series, ([0, 0],), operator.methodcaller("round")),
@@ -205,7 +200,6 @@
205200
),
206201
pytest.param(
207202
(pd.DataFrame, frame_data, operator.methodcaller("round", 2)),
208-
marks=not_implemented_mark,
209203
),
210204
pytest.param(
211205
(pd.DataFrame, frame_data, operator.methodcaller("corr")),
@@ -228,12 +222,10 @@
228222
),
229223
pytest.param(
230224
(pd.DataFrame, frame_data, operator.methodcaller("count")),
231-
marks=not_implemented_mark,
232225
),
233226
pytest.param(
234227
(pd.DataFrame, frame_mi_data, operator.methodcaller("count", level="A")),
235228
marks=[
236-
not_implemented_mark,
237229
pytest.mark.filterwarnings("ignore:Using the level keyword:FutureWarning"),
238230
],
239231
),
@@ -261,7 +253,6 @@
261253
),
262254
pytest.param(
263255
(pd.DataFrame, frame_data, operator.methodcaller("quantile", q=[0.25, 0.75])),
264-
marks=not_implemented_mark,
265256
),
266257
pytest.param(
267258
(pd.DataFrame, frame_data, operator.methodcaller("quantile")),
@@ -279,19 +270,16 @@
279270
),
280271
pytest.param(
281272
(pd.DataFrame, frame_mi_data, operator.methodcaller("isin", [1])),
282-
marks=not_implemented_mark,
283273
),
284274
pytest.param(
285275
(pd.DataFrame, frame_mi_data, operator.methodcaller("isin", pd.Series([1]))),
286-
marks=not_implemented_mark,
287276
),
288277
pytest.param(
289278
(
290279
pd.DataFrame,
291280
frame_mi_data,
292281
operator.methodcaller("isin", pd.DataFrame({"A": [1]})),
293282
),
294-
marks=not_implemented_mark,
295283
),
296284
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
297285
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
@@ -300,10 +288,7 @@
300288
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
301289
marks=not_implemented_mark,
302290
),
303-
pytest.param(
304-
(pd.Series, ([1, 2],), operator.methodcaller("squeeze"))
305-
# marks=not_implemented_mark,
306-
),
291+
(pd.Series, ([1, 2],), operator.methodcaller("squeeze")),
307292
(pd.Series, ([1, 2],), operator.methodcaller("rename_axis", index="a")),
308293
(pd.DataFrame, frame_data, operator.methodcaller("rename_axis", columns="a")),
309294
# Unary ops
@@ -315,7 +300,7 @@
315300
(pd.Series, [1], operator.inv),
316301
(pd.DataFrame, frame_data, abs),
317302
(pd.Series, [1], abs),
318-
pytest.param((pd.DataFrame, frame_data, round), marks=not_implemented_mark),
303+
pytest.param((pd.DataFrame, frame_data, round)),
319304
(pd.Series, [1], round),
320305
(pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])),
321306
(pd.DataFrame, frame_mi_data, operator.methodcaller("xs", "a")),
@@ -461,10 +446,7 @@
461446
marks=not_implemented_mark,
462447
),
463448
(pd.Series, ([1, 2],), operator.methodcaller("pct_change")),
464-
pytest.param(
465-
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
466-
marks=not_implemented_mark,
467-
),
449+
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
468450
(pd.Series, ([1],), operator.methodcaller("transform", lambda x: x - x.min())),
469451
pytest.param(
470452
(

0 commit comments

Comments
 (0)