Skip to content

Commit db4d6be

Browse files
Backport PR #36532: BUG: Fix issue in preserving index name on empty DataFrame (#36545)
Co-authored-by: Irv Lustig <[email protected]>
1 parent 4d5ff7e commit db4d6be

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Fixed regressions
3535
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
3636
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`,:issue:`35802`)
3737
- Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`,:issue:`36377`)
38+
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)
3839

3940
.. ---------------------------------------------------------------------------
4041

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,8 @@ def _ensure_valid_index(self, value):
31723172

31733173
# GH31368 preserve name of index
31743174
index_copy = value.index.copy()
3175-
index_copy.name = self.index.name
3175+
if self.index.name is not None:
3176+
index_copy.name = self.index.name
31763177

31773178
self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan)
31783179

pandas/tests/indexing/test_partial.py

+8
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,11 @@ def test_index_name_empty(self):
672672
)
673673

674674
tm.assert_frame_equal(df, expected)
675+
676+
# GH 36527
677+
df = pd.DataFrame()
678+
series = pd.Series(1.23, index=pd.RangeIndex(4, name="series_index"))
679+
df["series"] = series
680+
expected = pd.DataFrame(
681+
{"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index")
682+
)

0 commit comments

Comments
 (0)