Skip to content

Commit 8f398e6

Browse files
Marco GorelliMarcoGorelli
authored andcommitted
🥅 raise more specific error if dict is appended to frame without ignore_index
1 parent 7d28040 commit 8f398e6

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,8 @@ Other
11891189
- Bug where :meth:`DataFrame.itertuples` would incorrectly determine whether or not namedtuples could be used for dataframes of 255 columns (:issue:`28282`)
11901190
- Handle nested NumPy ``object`` arrays in :func:`testing.assert_series_equal` for ExtensionArray implementations (:issue:`30841`)
11911191
- Bug in :class:`Index` constructor incorrectly allowing 2-dimensional input arrays (:issue:`13601`, :issue:`27125`)
1192+
- Appending a dictionary to a :class:`DataFrame` without passing ``ignore_index=True`` will raise ``TypeError: Can only append a dict if ignore_index=True``
1193+
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue`30871`)
11921194

11931195
.. ---------------------------------------------------------------------------
11941196

pandas/core/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7062,6 +7062,8 @@ def append(
70627062
"""
70637063
if isinstance(other, (Series, dict)):
70647064
if isinstance(other, dict):
7065+
if not ignore_index:
7066+
raise TypeError("Can only append a dict if ignore_index=True")
70657067
other = Series(other)
70667068
if other.name is None and not ignore_index:
70677069
raise TypeError(

pandas/tests/frame/methods/test_append.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def test_append_series_dict(self):
5050
)
5151
tm.assert_frame_equal(result, expected.loc[:, result.columns])
5252

53+
msg = "Can only append a dict if ignore_index=True"
54+
with pytest.raises(TypeError, match=msg):
55+
df.append(series.to_dict())
56+
5357
# can append when name set
5458
row = df.loc[4]
5559
row.name = 5

0 commit comments

Comments
 (0)