Skip to content

Commit f6df343

Browse files
DEPR: Series/DataFrame.append (pandas-dev#35407) (pandas-dev#44539)
1 parent 1a966b1 commit f6df343

30 files changed

+302
-283
lines changed

doc/source/getting_started/comparison/comparison_with_spreadsheets.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,14 @@ The equivalent in pandas:
435435
Adding a row
436436
~~~~~~~~~~~~
437437

438-
Assuming we are using a :class:`~pandas.RangeIndex` (numbered ``0``, ``1``, etc.), we can use :meth:`DataFrame.append` to add a row to the bottom of a ``DataFrame``.
438+
Assuming we are using a :class:`~pandas.RangeIndex` (numbered ``0``, ``1``, etc.), we can use :func:`concat` to add a row to the bottom of a ``DataFrame``.
439439

440440
.. ipython:: python
441441
442442
df
443-
new_row = {"class": "E", "student_count": 51, "all_pass": True}
444-
df.append(new_row, ignore_index=True)
443+
new_row = pd.DataFrame([["E", 51, True]],
444+
columns=["class", "student_count", "all_pass"])
445+
pd.concat([df, new_row])
445446
446447
447448
Find and Replace

doc/source/user_guide/10min.rst

-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ Concatenating pandas objects together with :func:`concat`:
478478
a row requires a copy, and may be expensive. We recommend passing a
479479
pre-built list of records to the :class:`DataFrame` constructor instead
480480
of building a :class:`DataFrame` by iteratively appending records to it.
481-
See :ref:`Appending to dataframe <merging.concatenation>` for more.
482481

483482
Join
484483
~~~~

doc/source/user_guide/cookbook.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ Valid frequency arguments to Grouper :ref:`Timeseries <timeseries.offset_aliases
929929
Merge
930930
-----
931931

932-
The :ref:`Concat <merging.concatenation>` docs. The :ref:`Join <merging.join>` docs.
932+
The :ref:`Join <merging.join>` docs.
933933

934-
`Append two dataframes with overlapping index (emulate R rbind)
934+
`Concatenate two dataframes with overlapping index (emulate R rbind)
935935
<https://stackoverflow.com/questions/14988480/pandas-version-of-rbind>`__
936936

937937
.. ipython:: python
@@ -944,7 +944,7 @@ Depending on df construction, ``ignore_index`` may be needed
944944

945945
.. ipython:: python
946946
947-
df = df1.append(df2, ignore_index=True)
947+
df = pd.concat([df1, df2], ignore_index=True)
948948
df
949949
950950
`Self Join of a DataFrame

doc/source/user_guide/merging.rst

+3-84
Original file line numberDiff line numberDiff line change
@@ -237,59 +237,6 @@ Similarly, we could index before the concatenation:
237237
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=False);
238238
plt.close("all");
239239
240-
.. _merging.concatenation:
241-
242-
Concatenating using ``append``
243-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244-
245-
A useful shortcut to :func:`~pandas.concat` are the :meth:`~DataFrame.append`
246-
instance methods on ``Series`` and ``DataFrame``. These methods actually predated
247-
``concat``. They concatenate along ``axis=0``, namely the index:
248-
249-
.. ipython:: python
250-
251-
result = df1.append(df2)
252-
253-
.. ipython:: python
254-
:suppress:
255-
256-
@savefig merging_append1.png
257-
p.plot([df1, df2], result, labels=["df1", "df2"], vertical=True);
258-
plt.close("all");
259-
260-
In the case of ``DataFrame``, the indexes must be disjoint but the columns do not
261-
need to be:
262-
263-
.. ipython:: python
264-
265-
result = df1.append(df4, sort=False)
266-
267-
.. ipython:: python
268-
:suppress:
269-
270-
@savefig merging_append2.png
271-
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=True);
272-
plt.close("all");
273-
274-
``append`` may take multiple objects to concatenate:
275-
276-
.. ipython:: python
277-
278-
result = df1.append([df2, df3])
279-
280-
.. ipython:: python
281-
:suppress:
282-
283-
@savefig merging_append3.png
284-
p.plot([df1, df2, df3], result, labels=["df1", "df2", "df3"], vertical=True);
285-
plt.close("all");
286-
287-
.. note::
288-
289-
Unlike the :py:meth:`~list.append` method, which appends to the original list
290-
and returns ``None``, :meth:`~DataFrame.append` here **does not** modify
291-
``df1`` and returns its copy with ``df2`` appended.
292-
293240
.. _merging.ignore_index:
294241

295242
Ignoring indexes on the concatenation axis
@@ -309,19 +256,6 @@ do this, use the ``ignore_index`` argument:
309256
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=True);
310257
plt.close("all");
311258
312-
This is also a valid argument to :meth:`DataFrame.append`:
313-
314-
.. ipython:: python
315-
316-
result = df1.append(df4, ignore_index=True, sort=False)
317-
318-
.. ipython:: python
319-
:suppress:
320-
321-
@savefig merging_append_ignore_index.png
322-
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=True);
323-
plt.close("all");
324-
325259
.. _merging.mixed_ndims:
326260

327261
Concatenating with mixed ndims
@@ -473,14 +407,13 @@ like GroupBy where the order of a categorical variable is meaningful.
473407
Appending rows to a DataFrame
474408
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
475409

476-
While not especially efficient (since a new object must be created), you can
477-
append a single row to a ``DataFrame`` by passing a ``Series`` or dict to
478-
``append``, which returns a new ``DataFrame`` as above.
410+
If you have a series that you want to append as a single row to a ``DataFrame``, you can convert the row into a
411+
``DataFrame`` and use ``concat``
479412

480413
.. ipython:: python
481414
482415
s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"])
483-
result = df1.append(s2, ignore_index=True)
416+
result = pd.concat([df1, s2.to_frame().T], ignore_index=True)
484417
485418
.. ipython:: python
486419
:suppress:
@@ -493,20 +426,6 @@ You should use ``ignore_index`` with this method to instruct DataFrame to
493426
discard its index. If you wish to preserve the index, you should construct an
494427
appropriately-indexed DataFrame and append or concatenate those objects.
495428

496-
You can also pass a list of dicts or Series:
497-
498-
.. ipython:: python
499-
500-
dicts = [{"A": 1, "B": 2, "C": 3, "X": 4}, {"A": 5, "B": 6, "C": 7, "Y": 8}]
501-
result = df1.append(dicts, ignore_index=True, sort=False)
502-
503-
.. ipython:: python
504-
:suppress:
505-
506-
@savefig merging_append_dits.png
507-
p.plot([df1, pd.DataFrame(dicts)], result, labels=["df1", "dicts"], vertical=True);
508-
plt.close("all");
509-
510429
.. _merging.join:
511430

512431
Database-style DataFrame or named Series joining/merging

doc/source/whatsnew/v0.6.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Version 0.6.1 (December 13, 2011)
66

77
New features
88
~~~~~~~~~~~~
9-
- Can :ref:`append single rows <merging.append.row>` (as Series) to a DataFrame
9+
- Can append single rows (as Series) to a DataFrame
1010
- Add Spearman and Kendall rank :ref:`correlation <computation.correlation>`
1111
options to Series.corr and DataFrame.corr (:issue:`428`)
1212
- :ref:`Added <indexing.basics.get_value>` ``get_value`` and ``set_value`` methods to

doc/source/whatsnew/v0.7.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ New features
1919
intersection of the other axes. Improves performance of ``Series.append`` and
2020
``DataFrame.append`` (:issue:`468`, :issue:`479`, :issue:`273`)
2121

22-
- :ref:`Can <merging.concatenation>` pass multiple DataFrames to
22+
- Can pass multiple DataFrames to
2323
``DataFrame.append`` to concatenate (stack) and multiple Series to
2424
``Series.append`` too
2525

doc/source/whatsnew/v1.4.0.rst

+44-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ ignored when finding the concatenated dtype. These are now consistently *not* i
275275
276276
df1 = pd.DataFrame({"bar": [pd.Timestamp("2013-01-01")]}, index=range(1))
277277
df2 = pd.DataFrame({"bar": np.nan}, index=range(1, 2))
278-
res = df1.append(df2)
278+
res = pd.concat([df1, df2])
279279
280280
Previously, the float-dtype in ``df2`` would be ignored so the result dtype would be ``datetime64[ns]``. As a result, the ``np.nan`` would be cast to ``NaT``.
281281

@@ -510,6 +510,49 @@ when given numeric data, but in the future, a :class:`NumericIndex` will be retu
510510
Out [4]: NumericIndex([1, 2, 3], dtype='uint64')
511511
512512
513+
.. _whatsnew_140.deprecations.frame_series_append:
514+
515+
Deprecated Frame.append and Series.append
516+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
517+
518+
:meth:`DataFrame.append` and :meth:`Series.append` have been deprecated and will be removed in Pandas 2.0.
519+
Use :func:`pandas.concat` instead (:issue:`35407`).
520+
521+
*Deprecated syntax*
522+
523+
.. code-block:: ipython
524+
525+
In [1]: pd.Series([1, 2]).append(pd.Series([3, 4])
526+
Out [1]:
527+
<stdin>:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
528+
0 1
529+
1 2
530+
0 3
531+
1 4
532+
dtype: int64
533+
534+
In [2]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
535+
In [3]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
536+
In [4]: df1.append(df2)
537+
Out [4]:
538+
<stdin>:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
539+
A B
540+
0 1 2
541+
1 3 4
542+
0 5 6
543+
1 7 8
544+
545+
*Recommended syntax*
546+
547+
.. ipython:: python
548+
549+
pd.concat([pd.Series([1, 2]), pd.Series([3, 4])])
550+
551+
df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
552+
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
553+
pd.concat([df1, df2])
554+
555+
513556
.. _whatsnew_140.deprecations.other:
514557

515558
Other Deprecations

pandas/core/frame.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -3261,9 +3261,10 @@ def memory_usage(self, index: bool = True, deep: bool = False) -> Series:
32613261
index=self.columns,
32623262
)
32633263
if index:
3264-
result = self._constructor_sliced(
3264+
index_memory_usage = self._constructor_sliced(
32653265
self.index.memory_usage(deep=deep), index=["Index"]
3266-
).append(result)
3266+
)
3267+
result = index_memory_usage._append(result)
32673268
return result
32683269

32693270
def transpose(self, *args, copy: bool = False) -> DataFrame:
@@ -9003,6 +9004,23 @@ def append(
90039004
3 3
90049005
4 4
90059006
"""
9007+
warnings.warn(
9008+
"The frame.append method is deprecated "
9009+
"and will be removed from pandas in a future version. "
9010+
"Use pandas.concat instead.",
9011+
FutureWarning,
9012+
stacklevel=find_stack_level(),
9013+
)
9014+
9015+
return self._append(other, ignore_index, verify_integrity, sort)
9016+
9017+
def _append(
9018+
self,
9019+
other,
9020+
ignore_index: bool = False,
9021+
verify_integrity: bool = False,
9022+
sort: bool = False,
9023+
) -> DataFrame:
90069024
combined_columns = None
90079025
if isinstance(other, (Series, dict)):
90089026
if isinstance(other, dict):
@@ -9728,7 +9746,9 @@ def c(x):
97289746
idx_diff = result_index.difference(correl.index)
97299747

97309748
if len(idx_diff) > 0:
9731-
correl = correl.append(Series([np.nan] * len(idx_diff), index=idx_diff))
9749+
correl = correl._append(
9750+
Series([np.nan] * len(idx_diff), index=idx_diff)
9751+
)
97329752

97339753
return correl
97349754

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,7 @@ def _setitem_with_indexer_missing(self, indexer, value):
19931993
df = df.infer_objects()
19941994
self.obj._mgr = df._mgr
19951995
else:
1996-
self.obj._mgr = self.obj.append(value)._mgr
1996+
self.obj._mgr = self.obj._append(value)._mgr
19971997
self.obj._maybe_update_cacher(clear=True)
19981998

19991999
def _ensure_iterable_column_indexer(self, column_indexer):

pandas/core/reshape/pivot.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def _add_margins(
287287
if not values and isinstance(table, ABCSeries):
288288
# If there are no values and the table is a series, then there is only
289289
# one column in the data. Compute grand margin and return it.
290-
return table.append(Series({key: grand_margin[margins_name]}))
290+
return table._append(Series({key: grand_margin[margins_name]}))
291291

292292
elif values:
293293
marginal_result_set = _generate_marginal_results(
@@ -325,7 +325,7 @@ def _add_margins(
325325
margin_dummy[cols] = margin_dummy[cols].apply(
326326
maybe_downcast_to_dtype, args=(dtype,)
327327
)
328-
result = result.append(margin_dummy)
328+
result = result._append(margin_dummy)
329329
result.index.names = row_names
330330

331331
return result
@@ -738,7 +738,7 @@ def _normalize(table, normalize, margins: bool, margins_name="All"):
738738

739739
elif normalize == "index":
740740
index_margin = index_margin / index_margin.sum()
741-
table = table.append(index_margin)
741+
table = table._append(index_margin)
742742
table = table.fillna(0)
743743
table.index = table_index
744744

@@ -747,7 +747,7 @@ def _normalize(table, normalize, margins: bool, margins_name="All"):
747747
index_margin = index_margin / index_margin.sum()
748748
index_margin.loc[margins_name] = 1
749749
table = concat([table, column_margin], axis=1)
750-
table = table.append(index_margin)
750+
table = table._append(index_margin)
751751

752752
table = table.fillna(0)
753753
table.index = table_index

pandas/core/series.py

+13
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,19 @@ def append(
28932893
...
28942894
ValueError: Indexes have overlapping values: [0, 1, 2]
28952895
"""
2896+
warnings.warn(
2897+
"The series.append method is deprecated "
2898+
"and will be removed from pandas in a future version. "
2899+
"Use pandas.concat instead.",
2900+
FutureWarning,
2901+
stacklevel=find_stack_level(),
2902+
)
2903+
2904+
return self._append(to_append, ignore_index, verify_integrity)
2905+
2906+
def _append(
2907+
self, to_append, ignore_index: bool = False, verify_integrity: bool = False
2908+
):
28962909
from pandas.core.reshape.concat import concat
28972910

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

0 commit comments

Comments
 (0)