diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index f17be825e1295..3e2267a69ed0d 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -529,7 +529,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 bug in metadata propagation incorrectly copying DataFrame columns as metadata when the column name overlaps with the metadata name (:issue:`37037`) -- Fixed metadata propagation in the :class:`Series.dt` and :class:`Series.str` accessors and :class:`DataFrame.duplicated` and ::class:`DataFrame.stack` methods (:issue:`28283`) +- Fixed metadata propagation in the :class:`Series.dt` and :class:`Series.str` accessors and :class:`DataFrame.duplicated` and :class:`DataFrame.stack` and :class:`DataFrame.unstack` and :class:`DataFrame.pivot` methods (:issue:`28283`) - Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`) - 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`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index eb150dd87347f..b1d2a7fb0bf7d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7256,7 +7256,9 @@ def unstack(self, level=-1, fill_value=None): """ from pandas.core.reshape.reshape import unstack - return unstack(self, level, fill_value) + result = unstack(self, level, fill_value) + + return result.__finalize__(self, method="unstack") @Appender(_shared_docs["melt"] % dict(caller="df.melt(", other="melt")) def melt( diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 9d2e5cbcc7b58..211d86c89cde7 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -154,10 +154,7 @@ ), marks=not_implemented_mark, ), - pytest.param( - (pd.DataFrame, frame_data, operator.methodcaller("pivot", columns="A")), - marks=not_implemented_mark, - ), + (pd.DataFrame, frame_data, operator.methodcaller("pivot", columns="A")), pytest.param( ( pd.DataFrame, @@ -171,10 +168,7 @@ (pd.DataFrame, frame_data, operator.methodcaller("explode", "A")), marks=not_implemented_mark, ), - pytest.param( - (pd.DataFrame, frame_mi_data, operator.methodcaller("unstack")), - marks=not_implemented_mark, - ), + (pd.DataFrame, frame_mi_data, operator.methodcaller("unstack")), pytest.param( ( pd.DataFrame,