Skip to content

Commit 9e7e7a2

Browse files
authored
BUG: DataFrame index name no longer resets after appending a list of series (#44123)
1 parent e2320f1 commit 9e7e7a2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ Reshaping
617617
- Bug in :func:`concat` which ignored the ``sort`` parameter (:issue:`43375`)
618618
- Fixed bug in :func:`merge` with :class:`MultiIndex` as column index for the ``on`` argument returning an error when assigning a column internally (:issue:`43734`)
619619
- Bug in :func:`crosstab` would fail when inputs are lists or tuples (:issue:`44076`)
620+
- Bug in :meth:`DataFrame.append` failing to retain ``index.name`` when appending a list of :class:`Series` objects (:issue:`44109`)
620621
- Fixed metadata propagation in :meth:`Dataframe.apply` method, consequently fixing the same issue for :meth:`Dataframe.transform`, :meth:`Dataframe.nunique` and :meth:`Dataframe.mode` (:issue:`28283`)
621622

622623
Sparse

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -9086,6 +9086,8 @@ def append(
90869086
pass
90879087
elif not isinstance(other[0], DataFrame):
90889088
other = DataFrame(other)
9089+
if self.index.name is not None and not ignore_index:
9090+
other.index.name = self.index.name
90899091

90909092
from pandas.core.reshape.concat import concat
90919093

pandas/tests/frame/methods/test_append.py

+20
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ def test_append_list_of_series_dicts(self):
9696
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
9797
tm.assert_frame_equal(result, expected)
9898

99+
def test_append_list_retain_index_name(self):
100+
df = DataFrame(
101+
[[1, 2], [3, 4]], index=pd.Index(["a", "b"], name="keepthisname")
102+
)
103+
104+
serc = Series([5, 6], name="c")
105+
106+
expected = DataFrame(
107+
[[1, 2], [3, 4], [5, 6]],
108+
index=pd.Index(["a", "b", "c"], name="keepthisname"),
109+
)
110+
111+
# append series
112+
result = df.append(serc)
113+
tm.assert_frame_equal(result, expected)
114+
115+
# append list of series
116+
result = df.append([serc])
117+
tm.assert_frame_equal(result, expected)
118+
99119
def test_append_missing_cols(self):
100120
# GH22252
101121
# exercise the conditional branch in append method where the data

0 commit comments

Comments
 (0)