Skip to content

Commit c1289e9

Browse files
DEPR: Series/DataFrame.append (pandas-dev#35407)
1 parent 52c9181 commit c1289e9

File tree

5 files changed

+195
-68
lines changed

5 files changed

+195
-68
lines changed

doc/source/whatsnew/v1.4.0.rst

+55
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,61 @@ when given numeric data, but in the future, a :class:`NumericIndex` will be retu
438438
Out [4]: NumericIndex([1, 2, 3], dtype='uint64')
439439
440440
441+
.. _whatsnew_140.deprecations.frame_series_append:
442+
443+
Deprecated Frame.append and Series.append
444+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
445+
446+
:meth:`DataFrame.append` and :meth:`Series.append` have been deprecated and will be removed in Pandas 2.0.
447+
Use :func:`pandas.core.reshape.concat` instead (:issue:`35407`).
448+
449+
*Deprecated syntax*
450+
451+
.. code-block:: ipython
452+
453+
In [1]: pd.Series([1, 2]).append(pd.Series([3, 4])
454+
Out [1]:
455+
<stdin>:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.core.reshape.concat instead.
456+
0 1
457+
1 2
458+
0 3
459+
1 4
460+
dtype: int64
461+
462+
In [2]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
463+
In [3]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
464+
In [4]: df1.append(df2)
465+
Out [4]:
466+
<stdin>:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.core.reshape.concat instead.
467+
A B
468+
0 1 2
469+
1 3 4
470+
0 5 6
471+
1 7 8
472+
473+
*Recommended syntax*
474+
475+
.. code-block:: ipython
476+
477+
In [1]: pd.core.reshape.concat.concat([pd.Series([1, 2]), pd.Series([3, 4])])
478+
Out [1]:
479+
0 1
480+
1 2
481+
0 3
482+
1 4
483+
dtype: int64
484+
485+
In [2]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
486+
In [3]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
487+
In [4]: pd.core.reshape.concat.concat([df1, df2])
488+
Out [4]:
489+
A B
490+
0 1 2
491+
1 3 4
492+
0 5 6
493+
1 7 8
494+
495+
441496
.. _whatsnew_140.deprecations.other:
442497

443498
Other Deprecations

pandas/core/frame.py

+8
Original file line numberDiff line numberDiff line change
@@ -9114,6 +9114,14 @@ def append(
91149114
3 3
91159115
4 4
91169116
"""
9117+
warnings.warn(
9118+
"The frame.append method is deprecated "
9119+
"and will be removed from pandas in a future version. "
9120+
"Use pandas.core.reshape.concat instead.",
9121+
FutureWarning,
9122+
stacklevel=2,
9123+
)
9124+
91179125
combined_columns = None
91189126
if isinstance(other, (Series, dict)):
91199127
if isinstance(other, dict):

pandas/core/series.py

+8
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,14 @@ def append(
28882888
...
28892889
ValueError: Indexes have overlapping values: [0, 1, 2]
28902890
"""
2891+
warnings.warn(
2892+
"The frame.append method is deprecated "
2893+
"and will be removed from pandas in a future version. "
2894+
"Use pandas.core.reshape.concat instead.",
2895+
FutureWarning,
2896+
stacklevel=2,
2897+
)
2898+
28912899
from pandas.core.reshape.concat import concat
28922900

28932901
if isinstance(to_append, (list, tuple)):

pandas/tests/frame/methods/test_append.py

+70-39
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ def test_append_multiindex(self, multiindex_dataframe_random_data, frame_or_seri
2121
a = obj[:5]
2222
b = obj[5:]
2323

24-
result = a.append(b)
24+
with tm.assert_produces_warning(FutureWarning):
25+
result = a.append(b)
2526
tm.assert_equal(result, obj)
2627

2728
def test_append_empty_list(self):
2829
# GH 28769
2930
df = DataFrame()
30-
result = df.append([])
31+
with tm.assert_produces_warning(FutureWarning):
32+
result = df.append([])
3133
expected = df
3234
tm.assert_frame_equal(result, expected)
3335
assert result is not df
3436

3537
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])
36-
result = df.append([])
38+
with tm.assert_produces_warning(FutureWarning):
39+
result = df.append([])
3740
expected = df
3841
tm.assert_frame_equal(result, expected)
3942
assert result is not df # .append() should return a new object
@@ -43,57 +46,69 @@ def test_append_series_dict(self):
4346

4447
series = df.loc[4]
4548
msg = "Indexes have overlapping values"
46-
with pytest.raises(ValueError, match=msg):
49+
with pytest.raises(ValueError, match=msg), tm.assert_produces_warning(
50+
FutureWarning
51+
):
4752
df.append(series, verify_integrity=True)
4853

4954
series.name = None
5055
msg = "Can only append a Series if ignore_index=True"
51-
with pytest.raises(TypeError, match=msg):
56+
with pytest.raises(TypeError, match=msg), tm.assert_produces_warning(
57+
FutureWarning
58+
):
5259
df.append(series, verify_integrity=True)
5360

54-
result = df.append(series[::-1], ignore_index=True)
55-
expected = df.append(
56-
DataFrame({0: series[::-1]}, index=df.columns).T, ignore_index=True
57-
)
61+
with tm.assert_produces_warning(FutureWarning):
62+
result = df.append(series[::-1], ignore_index=True)
63+
expected = df.append(
64+
DataFrame({0: series[::-1]}, index=df.columns).T, ignore_index=True
65+
)
5866
tm.assert_frame_equal(result, expected)
5967

6068
# dict
61-
result = df.append(series.to_dict(), ignore_index=True)
69+
with tm.assert_produces_warning(FutureWarning):
70+
result = df.append(series.to_dict(), ignore_index=True)
6271
tm.assert_frame_equal(result, expected)
6372

64-
result = df.append(series[::-1][:3], ignore_index=True)
65-
expected = df.append(
66-
DataFrame({0: series[::-1][:3]}).T, ignore_index=True, sort=True
67-
)
73+
with tm.assert_produces_warning(FutureWarning):
74+
result = df.append(series[::-1][:3], ignore_index=True)
75+
expected = df.append(
76+
DataFrame({0: series[::-1][:3]}).T, ignore_index=True, sort=True
77+
)
6878
tm.assert_frame_equal(result, expected.loc[:, result.columns])
6979

7080
msg = "Can only append a dict if ignore_index=True"
71-
with pytest.raises(TypeError, match=msg):
81+
with pytest.raises(TypeError, match=msg), tm.assert_produces_warning(
82+
FutureWarning
83+
):
7284
df.append(series.to_dict())
7385

7486
# can append when name set
7587
row = df.loc[4]
7688
row.name = 5
77-
result = df.append(row)
78-
expected = df.append(df[-1:], ignore_index=True)
89+
with tm.assert_produces_warning(FutureWarning):
90+
result = df.append(row)
91+
expected = df.append(df[-1:], ignore_index=True)
7992
tm.assert_frame_equal(result, expected)
8093

8194
def test_append_list_of_series_dicts(self):
8295
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])
8396

8497
dicts = [x.to_dict() for idx, x in df.iterrows()]
8598

86-
result = df.append(dicts, ignore_index=True)
87-
expected = df.append(df, ignore_index=True)
99+
with tm.assert_produces_warning(FutureWarning):
100+
result = df.append(dicts, ignore_index=True)
101+
expected = df.append(df, ignore_index=True)
88102
tm.assert_frame_equal(result, expected)
89103

90104
# different columns
91105
dicts = [
92106
{"foo": 1, "bar": 2, "baz": 3, "peekaboo": 4},
93107
{"foo": 5, "bar": 6, "baz": 7, "peekaboo": 8},
94108
]
95-
result = df.append(dicts, ignore_index=True, sort=True)
96-
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
109+
with tm.assert_produces_warning(FutureWarning):
110+
result = df.append(dicts, ignore_index=True, sort=True)
111+
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
97112
tm.assert_frame_equal(result, expected)
98113

99114
def test_append_list_retain_index_name(self):
@@ -109,11 +124,13 @@ def test_append_list_retain_index_name(self):
109124
)
110125

111126
# append series
112-
result = df.append(serc)
127+
with tm.assert_produces_warning(FutureWarning):
128+
result = df.append(serc)
113129
tm.assert_frame_equal(result, expected)
114130

115131
# append list of series
116-
result = df.append([serc])
132+
with tm.assert_produces_warning(FutureWarning):
133+
result = df.append([serc])
117134
tm.assert_frame_equal(result, expected)
118135

119136
def test_append_missing_cols(self):
@@ -124,39 +141,43 @@ def test_append_missing_cols(self):
124141
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])
125142

126143
dicts = [{"foo": 9}, {"bar": 10}]
127-
with tm.assert_produces_warning(None):
144+
with tm.assert_produces_warning(FutureWarning):
128145
result = df.append(dicts, ignore_index=True, sort=True)
129146

130-
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
147+
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
131148
tm.assert_frame_equal(result, expected)
132149

133150
def test_append_empty_dataframe(self):
134151

135152
# Empty df append empty df
136153
df1 = DataFrame()
137154
df2 = DataFrame()
138-
result = df1.append(df2)
155+
with tm.assert_produces_warning(FutureWarning):
156+
result = df1.append(df2)
139157
expected = df1.copy()
140158
tm.assert_frame_equal(result, expected)
141159

142160
# Non-empty df append empty df
143161
df1 = DataFrame(np.random.randn(5, 2))
144162
df2 = DataFrame()
145-
result = df1.append(df2)
163+
with tm.assert_produces_warning(FutureWarning):
164+
result = df1.append(df2)
146165
expected = df1.copy()
147166
tm.assert_frame_equal(result, expected)
148167

149168
# Empty df with columns append empty df
150169
df1 = DataFrame(columns=["bar", "foo"])
151170
df2 = DataFrame()
152-
result = df1.append(df2)
171+
with tm.assert_produces_warning(FutureWarning):
172+
result = df1.append(df2)
153173
expected = df1.copy()
154174
tm.assert_frame_equal(result, expected)
155175

156176
# Non-Empty df with columns append empty df
157177
df1 = DataFrame(np.random.randn(5, 2), columns=["bar", "foo"])
158178
df2 = DataFrame()
159-
result = df1.append(df2)
179+
with tm.assert_produces_warning(FutureWarning):
180+
result = df1.append(df2)
160181
expected = df1.copy()
161182
tm.assert_frame_equal(result, expected)
162183

@@ -168,19 +189,22 @@ def test_append_dtypes(self):
168189

169190
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(5))
170191
df2 = DataFrame()
171-
result = df1.append(df2)
192+
with tm.assert_produces_warning(FutureWarning):
193+
result = df1.append(df2)
172194
expected = df1.copy()
173195
tm.assert_frame_equal(result, expected)
174196

175197
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))
176198
df2 = DataFrame({"bar": "foo"}, index=range(1, 2))
177-
result = df1.append(df2)
199+
with tm.assert_produces_warning(FutureWarning):
200+
result = df1.append(df2)
178201
expected = DataFrame({"bar": [Timestamp("20130101"), "foo"]})
179202
tm.assert_frame_equal(result, expected)
180203

181204
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))
182205
df2 = DataFrame({"bar": np.nan}, index=range(1, 2))
183-
result = df1.append(df2)
206+
with tm.assert_produces_warning(FutureWarning):
207+
result = df1.append(df2)
184208
expected = DataFrame(
185209
{"bar": Series([Timestamp("20130101"), np.nan], dtype="M8[ns]")}
186210
)
@@ -189,7 +213,8 @@ def test_append_dtypes(self):
189213

190214
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))
191215
df2 = DataFrame({"bar": np.nan}, index=range(1, 2), dtype=object)
192-
result = df1.append(df2)
216+
with tm.assert_produces_warning(FutureWarning):
217+
result = df1.append(df2)
193218
expected = DataFrame(
194219
{"bar": Series([Timestamp("20130101"), np.nan], dtype="M8[ns]")}
195220
)
@@ -198,7 +223,8 @@ def test_append_dtypes(self):
198223

199224
df1 = DataFrame({"bar": np.nan}, index=range(1))
200225
df2 = DataFrame({"bar": Timestamp("20130101")}, index=range(1, 2))
201-
result = df1.append(df2)
226+
with tm.assert_produces_warning(FutureWarning):
227+
result = df1.append(df2)
202228
expected = DataFrame(
203229
{"bar": Series([np.nan, Timestamp("20130101")], dtype="M8[ns]")}
204230
)
@@ -207,7 +233,8 @@ def test_append_dtypes(self):
207233

208234
df1 = DataFrame({"bar": Timestamp("20130101")}, index=range(1))
209235
df2 = DataFrame({"bar": 1}, index=range(1, 2), dtype=object)
210-
result = df1.append(df2)
236+
with tm.assert_produces_warning(FutureWarning):
237+
result = df1.append(df2)
211238
expected = DataFrame({"bar": Series([Timestamp("20130101"), 1])})
212239
tm.assert_frame_equal(result, expected)
213240

@@ -218,7 +245,8 @@ def test_append_timestamps_aware_or_naive(self, tz_naive_fixture, timestamp):
218245
# GH 30238
219246
tz = tz_naive_fixture
220247
df = DataFrame([Timestamp(timestamp, tz=tz)])
221-
result = df.append(df.iloc[0]).iloc[-1]
248+
with tm.assert_produces_warning(FutureWarning):
249+
result = df.append(df.iloc[0]).iloc[-1]
222250
expected = Series(Timestamp(timestamp, tz=tz), name=0)
223251
tm.assert_series_equal(result, expected)
224252

@@ -234,7 +262,8 @@ def test_append_timestamps_aware_or_naive(self, tz_naive_fixture, timestamp):
234262
)
235263
def test_other_dtypes(self, data, dtype):
236264
df = DataFrame(data, dtype=dtype)
237-
result = df.append(df.iloc[0]).iloc[-1]
265+
with tm.assert_produces_warning(FutureWarning):
266+
result = df.append(df.iloc[0]).iloc[-1]
238267
expected = Series(data, name=0, dtype=dtype)
239268
tm.assert_series_equal(result, expected)
240269

@@ -249,7 +278,8 @@ def test_append_numpy_bug_1681(self, dtype):
249278
df = DataFrame()
250279
other = DataFrame({"A": "foo", "B": index}, index=index)
251280

252-
result = df.append(other)
281+
with tm.assert_produces_warning(FutureWarning):
282+
result = df.append(other)
253283
assert (result["B"] == index).all()
254284

255285
@pytest.mark.filterwarnings("ignore:The values in the array:RuntimeWarning")
@@ -264,7 +294,8 @@ def test_multiindex_column_append_multiple(self):
264294
df2 = df.copy()
265295
for i in range(1, 10):
266296
df[i, "colA"] = 10
267-
df = df.append(df2, ignore_index=True)
297+
with tm.assert_produces_warning(FutureWarning):
298+
df = df.append(df2, ignore_index=True)
268299
result = df["multi"]
269300
expected = DataFrame(
270301
{"col1": [1, 2, 3] * (i + 1), "col2": [11, 12, 13] * (i + 1)}

0 commit comments

Comments
 (0)