From d1469544ed128d971b54414639111c24bcee8873 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sat, 17 Oct 2020 15:11:01 -0500 Subject: [PATCH] BUG: Don't copy DataFrame columns as metadata See https://github.com/pandas-dev/pandas/pull/37186/files#r506978889. Basically, we don't want to accidentally do `series.name = dataframe.name` and try to set a column. --- doc/source/whatsnew/v1.2.0.rst | 1 + pandas/core/generic.py | 2 +- pandas/tests/generic/test_finalize.py | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index d08e8e009811a..54ed605bf87e4 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -483,6 +483,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 (: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/generic.py b/pandas/core/generic.py index 3079bb0b79b33..48044831d7ad6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5346,7 +5346,7 @@ def __finalize__( self.flags.allows_duplicate_labels = other.flags.allows_duplicate_labels # For subclasses using _metadata. - for name in self._metadata: + for name in set(self._metadata) & set(other._metadata): assert isinstance(name, str) object.__setattr__(self, name, getattr(other, name, None)) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 25c926b1de4c6..7be5837dac158 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -772,3 +772,11 @@ def test_groupby(obj, method): obj.attrs = {"a": 1} result = method(obj.groupby([0, 0])) assert result.attrs == {"a": 1} + + +def test_finalize_frame_series_name(): + # https://github.com/pandas-dev/pandas/pull/37186/files#r506978889 + # ensure we don't copy the column `name` to the Series. + df = pd.DataFrame({"name": [1, 2]}) + result = pd.Series([1, 2]).__finalize__(df) + assert result.name is None