Skip to content

Commit e639af2

Browse files
asishmjreback
authored andcommitted
BUG: Appending empty list to DataFrame #28769 (#28834)
1 parent 29be383 commit e639af2

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ Other
465465
- :meth:`Series.append` will no longer raise a ``TypeError`` when passed a tuple of ``Series`` (:issue:`28410`)
466466
- :meth:`SeriesGroupBy.value_counts` will be able to handle the case even when the :class:`Grouper` makes empty groups (:issue: 28479)
467467
- Fix corrupted error message when calling ``pandas.libs._json.encode()`` on a 0d array (:issue:`18878`)
468+
- Bug in :meth:`DataFrame.append` that raised ``IndexError`` when appending with empty list (:issue:`28769`)
468469
- Fix :class:`AbstractHolidayCalendar` to return correct results for
469470
years after 2030 (now goes up to 2200) (:issue:`27790`)
470471

pandas/core/frame.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -6943,10 +6943,13 @@ def append(self, other, ignore_index=False, verify_integrity=False, sort=None):
69436943
other = other._convert(datetime=True, timedelta=True)
69446944
if not self.columns.equals(combined_columns):
69456945
self = self.reindex(columns=combined_columns)
6946-
elif isinstance(other, list) and not isinstance(other[0], DataFrame):
6947-
other = DataFrame(other)
6948-
if (self.columns.get_indexer(other.columns) >= 0).all():
6949-
other = other.reindex(columns=self.columns)
6946+
elif isinstance(other, list):
6947+
if not other:
6948+
pass
6949+
elif not isinstance(other[0], DataFrame):
6950+
other = DataFrame(other)
6951+
if (self.columns.get_indexer(other.columns) >= 0).all():
6952+
other = other.reindex(columns=self.columns)
69506953

69516954
from pandas.core.reshape.concat import concat
69526955

pandas/tests/frame/test_combine_concat.py

+14
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ def test_concat_tuple_keys(self):
128128
)
129129
tm.assert_frame_equal(results, expected)
130130

131+
def test_append_empty_list(self):
132+
# GH 28769
133+
df = DataFrame()
134+
result = df.append([])
135+
expected = df
136+
tm.assert_frame_equal(result, expected)
137+
assert result is not df
138+
139+
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])
140+
result = df.append([])
141+
expected = df
142+
tm.assert_frame_equal(result, expected)
143+
assert result is not df # .append() should return a new object
144+
131145
def test_append_series_dict(self):
132146
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])
133147

0 commit comments

Comments
 (0)