diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2a34c71412789..cc9a980a7c7d4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9478,7 +9478,7 @@ def _series_round(ser: Series, decimals: int): if len(new_cols) > 0: return self._constructor( concat(new_cols, axis=1), index=self.index, columns=self.columns - ) + ).__finalize__(self, method="round") else: return self @@ -9866,7 +9866,8 @@ def count( FutureWarning, stacklevel=find_stack_level(), ) - return self._count_level(level, axis=axis, numeric_only=numeric_only) + res = self._count_level(level, axis=axis, numeric_only=numeric_only) + return res.__finalize__(self, method="count") if numeric_only: frame = self._get_numeric_data() @@ -9889,7 +9890,7 @@ def count( counts, index=frame._get_agg_axis(axis) ) - return result.astype("int64") + return result.astype("int64").__finalize__(self, method="count") def _count_level(self, level: Level, axis: int = 0, numeric_only: bool = False): if numeric_only: @@ -10519,13 +10520,14 @@ def quantile( dtype = cdtype if is_list_like(q): - return self._constructor([], index=q, columns=cols, dtype=dtype) + res = self._constructor([], index=q, columns=cols, dtype=dtype) + return res.__finalize__(self, method="quantile") return self._constructor_sliced([], index=cols, name=q, dtype=dtype) res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation) result = self._constructor(res) - return result + return result.__finalize__(self, method="quantile") @doc(NDFrame.asfreq, **_shared_doc_kwargs) def asfreq( @@ -10742,7 +10744,7 @@ def isin(self, values) -> DataFrame: from pandas.core.reshape.concat import concat values = collections.defaultdict(list, values) - return concat( + result = concat( ( self.iloc[:, [i]].isin(values[col]) for i, col in enumerate(self.columns) @@ -10752,11 +10754,11 @@ def isin(self, values) -> DataFrame: elif isinstance(values, Series): if not values.index.is_unique: raise ValueError("cannot compute isin with a duplicate axis.") - return self.eq(values.reindex_like(self), axis="index") + result = self.eq(values.reindex_like(self), axis="index") elif isinstance(values, DataFrame): if not (values.columns.is_unique and values.index.is_unique): raise ValueError("cannot compute isin with a duplicate axis.") - return self.eq(values.reindex_like(self)) + result = self.eq(values.reindex_like(self)) else: if not is_list_like(values): raise TypeError( @@ -10764,11 +10766,12 @@ def isin(self, values) -> DataFrame: "to be passed to DataFrame.isin(), " f"you passed a '{type(values).__name__}'" ) - return self._constructor( + result = self._constructor( algorithms.isin(self.values.ravel(), values).reshape(self.shape), self.index, self.columns, ) + return result.__finalize__(self, method="isin") # ---------------------------------------------------------------------- # Add index and columns diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4971c7f34cfdb..a497475ebd182 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1646,7 +1646,7 @@ def __abs__(self: NDFrameT) -> NDFrameT: @final def __round__(self: NDFrameT, decimals: int = 0) -> NDFrameT: - return self.round(decimals) + return self.round(decimals).__finalize__(self, method="__round__") # ------------------------------------------------------------------------- # Label or Level Combination Helpers @@ -10383,7 +10383,7 @@ def pct_change( # We want to restore the original index rs = rs.loc[~rs.index.duplicated()] rs = rs.reindex_like(data) - return rs + return rs.__finalize__(self, method="pct_change") @final def _agg_by_level( diff --git a/pandas/core/series.py b/pandas/core/series.py index 596953652d2ff..614f3525bb539 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1799,7 +1799,8 @@ def to_frame(self, name: Hashable = lib.no_default) -> DataFrame: columns = Index([name]) mgr = self._mgr.to_2d_mgr(columns) - return self._constructor_expanddim(mgr) + df = self._constructor_expanddim(mgr) + return df.__finalize__(self, method="to_frame") def _set_name(self, name, inplace=False) -> Series: """ diff --git a/pandas/tests/generic/test_duplicate_labels.py b/pandas/tests/generic/test_duplicate_labels.py index 43cd3039870d0..189c5382ef114 100644 --- a/pandas/tests/generic/test_duplicate_labels.py +++ b/pandas/tests/generic/test_duplicate_labels.py @@ -63,10 +63,9 @@ def test_preserved_frame(self): assert df.loc[["a"]].flags.allows_duplicate_labels is False assert df.loc[:, ["A", "B"]].flags.allows_duplicate_labels is False - @not_implemented def test_to_frame(self): - s = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False) - assert s.to_frame().flags.allows_duplicate_labels is False + ser = pd.Series(dtype=float).set_flags(allows_duplicate_labels=False) + assert ser.to_frame().flags.allows_duplicate_labels is False @pytest.mark.parametrize("func", ["add", "sub"]) @pytest.mark.parametrize( diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 403e5c6c7daf7..cf92cd55a720e 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -38,14 +38,9 @@ (pd.Series, ([0],), operator.methodcaller("take", [])), (pd.Series, ([0],), operator.methodcaller("__getitem__", [True])), (pd.Series, ([0],), operator.methodcaller("repeat", 2)), - pytest.param( - (pd.Series, ([0],), operator.methodcaller("reset_index")), - marks=pytest.mark.xfail, - ), + (pd.Series, ([0],), operator.methodcaller("reset_index")), (pd.Series, ([0],), operator.methodcaller("reset_index", drop=True)), - pytest.param( - (pd.Series, ([0],), operator.methodcaller("to_frame")), marks=pytest.mark.xfail - ), + (pd.Series, ([0],), operator.methodcaller("to_frame")), (pd.Series, ([0, 0],), operator.methodcaller("drop_duplicates")), (pd.Series, ([0, 0],), operator.methodcaller("duplicated")), (pd.Series, ([0, 0],), operator.methodcaller("round")), @@ -205,7 +200,6 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("round", 2)), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("corr")), @@ -228,12 +222,10 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("count")), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_mi_data, operator.methodcaller("count", level="A")), marks=[ - not_implemented_mark, pytest.mark.filterwarnings("ignore:Using the level keyword:FutureWarning"), ], ), @@ -261,7 +253,6 @@ ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("quantile", q=[0.25, 0.75])), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("quantile")), @@ -279,11 +270,9 @@ ), pytest.param( (pd.DataFrame, frame_mi_data, operator.methodcaller("isin", [1])), - marks=not_implemented_mark, ), pytest.param( (pd.DataFrame, frame_mi_data, operator.methodcaller("isin", pd.Series([1]))), - marks=not_implemented_mark, ), pytest.param( ( @@ -291,7 +280,6 @@ frame_mi_data, operator.methodcaller("isin", pd.DataFrame({"A": [1]})), ), - marks=not_implemented_mark, ), (pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)), (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), @@ -300,10 +288,7 @@ (pd.DataFrame, frame_data, operator.methodcaller("squeeze")), marks=not_implemented_mark, ), - pytest.param( - (pd.Series, ([1, 2],), operator.methodcaller("squeeze")) - # marks=not_implemented_mark, - ), + (pd.Series, ([1, 2],), operator.methodcaller("squeeze")), (pd.Series, ([1, 2],), operator.methodcaller("rename_axis", index="a")), (pd.DataFrame, frame_data, operator.methodcaller("rename_axis", columns="a")), # Unary ops @@ -315,7 +300,7 @@ (pd.Series, [1], operator.inv), (pd.DataFrame, frame_data, abs), (pd.Series, [1], abs), - pytest.param((pd.DataFrame, frame_data, round), marks=not_implemented_mark), + pytest.param((pd.DataFrame, frame_data, round)), (pd.Series, [1], round), (pd.DataFrame, frame_data, operator.methodcaller("take", [0, 0])), (pd.DataFrame, frame_mi_data, operator.methodcaller("xs", "a")), @@ -461,10 +446,7 @@ marks=not_implemented_mark, ), (pd.Series, ([1, 2],), operator.methodcaller("pct_change")), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("pct_change")), - marks=not_implemented_mark, - ), + (pd.DataFrame, frame_data, operator.methodcaller("pct_change")), (pd.Series, ([1],), operator.methodcaller("transform", lambda x: x - x.min())), pytest.param( (