Skip to content

Commit f59700b

Browse files
authored
BUG: Fix issue in preserving index name on empty DataFrame (#36532)
1 parent 84ac62f commit f59700b

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
@@ -3190,7 +3190,8 @@ def _ensure_valid_index(self, value):
31903190

31913191
# GH31368 preserve name of index
31923192
index_copy = value.index.copy()
3193-
index_copy.name = self.index.name
3193+
if self.index.name is not None:
3194+
index_copy.name = self.index.name
31943195

31953196
self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan)
31963197

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)