Skip to content

Commit 2dc4b3e

Browse files
author
Marco Gorelli
committed
🥅 raise more specific error if dict is appended to frame without ignore_index
1 parent b7fcb54 commit 2dc4b3e

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
@@ -1170,6 +1170,8 @@ Other
11701170
- Bug where :meth:`DataFrame.itertuples` would incorrectly determine whether or not namedtuples could be used for dataframes of 255 columns (:issue:`28282`)
11711171
- Handle nested NumPy ``object`` arrays in :func:`testing.assert_series_equal` for ExtensionArray implementations (:issue:`30841`)
11721172
- Bug in :class:`Index` constructor incorrectly allowing 2-dimensional input arrays (:issue:`13601`, :issue:`27125`)
1173+
- Appending a dictionary to a :class:`DataFrame` without passing ``ignore_index=True`` will raise ``TypeError: Can only append a dict if ignore_index=True``
1174+
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue`30871`)
11731175

11741176
.. ---------------------------------------------------------------------------
11751177

pandas/core/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7023,6 +7023,8 @@ def append(
70237023
"""
70247024
if isinstance(other, (Series, dict)):
70257025
if isinstance(other, dict):
7026+
if not ignore_index:
7027+
raise TypeError("Can only append a dict if ignore_index=True")
70267028
other = Series(other)
70277029
if other.name is None and not ignore_index:
70287030
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)