From cbae7866a51a42aab5d9c52ee0b695efe55df0d7 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 21 Sep 2020 16:13:10 -0400 Subject: [PATCH 1/2] BUG: Fix issue in preserving index name on empty DataFrame --- doc/source/whatsnew/v1.1.3.rst | 1 + pandas/core/frame.py | 3 ++- pandas/tests/indexing/test_partial.py | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index 72937141c2870..e3a96c69918db 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -35,6 +35,7 @@ Fixed regressions - Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`) - Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`,:issue:`35802`) - Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`,:issue:`36377`) +- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 36dfe43bfd708..69b12bcff967f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3190,7 +3190,8 @@ def _ensure_valid_index(self, value): # GH31368 preserve name of index index_copy = value.index.copy() - index_copy.name = self.index.name + if self.index.name is not None: + index_copy.name = self.index.name self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan) diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 7afbbc2b9ab2b..b170ad30d4b0b 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -672,3 +672,12 @@ def test_index_name_empty(self): ) tm.assert_frame_equal(df, expected) + + # GH 36527 + df = pd.DataFrame() + series = pd.Series(1.23, index=pd.RangeIndex(4, name="series_index")) + df["series"] = series + expected = pd.DataFrame( + {"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index") + ) + From bd99a3b5f612c1dd493537ed3738518c1f06b400 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Mon, 21 Sep 2020 16:17:28 -0400 Subject: [PATCH 2/2] remove blank line in testing file --- pandas/tests/indexing/test_partial.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index b170ad30d4b0b..72bc13e67c040 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -680,4 +680,3 @@ def test_index_name_empty(self): expected = pd.DataFrame( {"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index") ) -