Skip to content

Commit 15bacea

Browse files
MarcoGorelliWillAyd
authored andcommitted
raise more specific error if dict is appended to frame wit… (#30882)
1 parent 7882aac commit 15bacea

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ ExtensionArray
159159

160160
Other
161161
^^^^^
162-
-
162+
- Appending a dictionary to a :class:`DataFrame` without passing ``ignore_index=True`` will raise ``TypeError: Can only append a dict if ignore_index=True``
163+
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
163164
-
164165

165166
.. ---------------------------------------------------------------------------

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -7102,6 +7102,8 @@ def append(
71027102
"""
71037103
if isinstance(other, (Series, dict)):
71047104
if isinstance(other, dict):
7105+
if not ignore_index:
7106+
raise TypeError("Can only append a dict if ignore_index=True")
71057107
other = Series(other)
71067108
if other.name is None and not ignore_index:
71077109
raise TypeError(

pandas/tests/frame/methods/test_append.py

+4
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)