Skip to content

DEPR: Series/DataFrame.append (#35407) #44539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c1289e9
DEPR: Series/DataFrame.append (#35407)
neinkeinkaffee Nov 20, 2021
7d96d25
Refer to pandas.concat instead of pandas.core.reshape.concat
neinkeinkaffee Nov 20, 2021
963d151
Add explicit test for warning, ignore in the remainder of the tests
neinkeinkaffee Nov 20, 2021
2b818bb
Refer to concat in root namespace
neinkeinkaffee Nov 20, 2021
153a784
Fix copy-paste errors
neinkeinkaffee Nov 20, 2021
98d0f9c
Ignore warning in reshape/concat test_append and add issue reference
neinkeinkaffee Nov 21, 2021
10ef0b6
Ignore warnings in reshape test_crosstab
neinkeinkaffee Nov 21, 2021
becc29f
Replace further appends with concat
neinkeinkaffee Nov 29, 2021
bdf2484
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Nov 29, 2021
02620b8
Ignore FutureWarning in test_value_counts_null
neinkeinkaffee Nov 30, 2021
bc9f6dc
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Nov 30, 2021
7b9fdf3
Filter FutureWarning in remaining affected tests
neinkeinkaffee Nov 30, 2021
bf2f30c
Ignore FutureWarning in even more tests
neinkeinkaffee Nov 30, 2021
982250d
Ignore FutureWarning in one last test
neinkeinkaffee Nov 30, 2021
fbdbc24
Delete refs to merging.concatenation and merging.append.row (document…
neinkeinkaffee Nov 30, 2021
506868b
Replace append by concat in tests instead of ignoring warnings
neinkeinkaffee Dec 1, 2021
525297b
Introduce intermediate variables
neinkeinkaffee Dec 1, 2021
e19b2e1
Use ipython instead of code block
neinkeinkaffee Dec 1, 2021
18f45fe
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Dec 1, 2021
ca851c4
Extract _append and replace append by _append/concat in tests with ig…
neinkeinkaffee Dec 2, 2021
2be9010
Reinsert modified instructions for appending one row to df (suggestion)
neinkeinkaffee Dec 3, 2021
f397503
Merge master
neinkeinkaffee Dec 3, 2021
028b012
Import concat from pandas in tests
neinkeinkaffee Dec 4, 2021
1d8f3c5
Fit call to concat on one line
neinkeinkaffee Dec 4, 2021
4107ec3
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Dec 4, 2021
d319b0f
Replace append by concat in _add_margins
neinkeinkaffee Dec 4, 2021
7f9cb7c
Merge commit '4bacee5a69f0e95c19638426553eb3fa24fe6c96' into 35407-de…
neinkeinkaffee Dec 5, 2021
46170d4
Replace append and _append by concat in tests
neinkeinkaffee Dec 5, 2021
caf1983
Import concat from pandas instead of locally from submodule
neinkeinkaffee Dec 5, 2021
aa90ca6
Test DataFrame._append where DataFrame.append is subject under test
neinkeinkaffee Dec 8, 2021
b673e4b
Extract variable and improve variable name
neinkeinkaffee Dec 8, 2021
e0492bc
Introduce Series._append and use in test_append and frame.py
neinkeinkaffee Dec 8, 2021
569abb1
Catch two more tests that should test _append
neinkeinkaffee Dec 8, 2021
c43ea63
Revert append -> concat and use _append internally instead
neinkeinkaffee Dec 8, 2021
0c99fab
Merge master
neinkeinkaffee Dec 24, 2021
f6586c8
Remove in-place append of single rows from the docs
neinkeinkaffee Dec 25, 2021
4e065f5
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Dec 26, 2021
430355b
Merge branch 'master' into 35407-deprecate-append
neinkeinkaffee Dec 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,61 @@ when given numeric data, but in the future, a :class:`NumericIndex` will be retu
Out [4]: NumericIndex([1, 2, 3], dtype='uint64')


.. _whatsnew_140.deprecations.frame_series_append:

Deprecated Frame.append and Series.append
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:meth:`DataFrame.append` and :meth:`Series.append` have been deprecated and will be removed in Pandas 2.0.
Use :func:`pandas.concat` instead (:issue:`35407`).

*Deprecated syntax*

.. code-block:: ipython

In [1]: pd.Series([1, 2]).append(pd.Series([3, 4])
Out [1]:
<stdin>:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
0 1
1 2
0 3
1 4
dtype: int64

In [2]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
In [3]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
In [4]: df1.append(df2)
Out [4]:
<stdin>:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
A B
0 1 2
1 3 4
0 5 6
1 7 8

*Recommended syntax*

.. code-block:: ipython
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont' use a code-block here, use an ipython-block (so it runs the code)


In [1]: pd.concat([pd.Series([1, 2]), pd.Series([3, 4])])
Out [1]:
0 1
1 2
0 3
1 4
dtype: int64

In [2]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
In [3]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
In [4]: pd.concat([df1, df2])
Out [4]:
A B
0 1 2
1 3 4
0 5 6
1 7 8


.. _whatsnew_140.deprecations.other:

Other Deprecations
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9114,6 +9114,14 @@ def append(
3 3
4 4
"""
warnings.warn(
"The frame.append method is deprecated "
"and will be removed from pandas in a future version. "
"Use pandas.concat instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

combined_columns = None
if isinstance(other, (Series, dict)):
if isinstance(other, dict):
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,14 @@ def append(
...
ValueError: Indexes have overlapping values: [0, 1, 2]
"""
warnings.warn(
"The series.append method is deprecated "
"and will be removed from pandas in a future version. "
"Use pandas.concat instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

from pandas.core.reshape.concat import concat

if isinstance(to_append, (list, tuple)):
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/frame/methods/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas._testing as tm


@pytest.mark.filterwarnings("ignore:.*append method is deprecated.*:FutureWarning")
class TestDataFrameAppend:
def test_append_multiindex(self, multiindex_dataframe_random_data, frame_or_series):
obj = multiindex_dataframe_random_data
Expand Down Expand Up @@ -124,8 +125,7 @@ def test_append_missing_cols(self):
df = DataFrame(np.random.randn(5, 4), columns=["foo", "bar", "baz", "qux"])

dicts = [{"foo": 9}, {"bar": 10}]
with tm.assert_produces_warning(None):
result = df.append(dicts, ignore_index=True, sort=True)
result = df.append(dicts, ignore_index=True, sort=True)

expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -270,3 +270,9 @@ def test_multiindex_column_append_multiple(self):
{"col1": [1, 2, 3] * (i + 1), "col2": [11, 12, 13] * (i + 1)}
)
tm.assert_frame_equal(result, expected)

def test_append_raises_future_warning(self):
df1 = DataFrame([[1, 2], [3, 4]])
df2 = DataFrame([[5, 6], [7, 8]])
with tm.assert_produces_warning(FutureWarning):
df1.append(df2)
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandas._testing as tm


@pytest.mark.filterwarnings("ignore:.*append method is deprecated.*:FutureWarning")
class TestSeriesAppend:
def test_append_preserve_name(self, datetime_series):
result = datetime_series[:5].append(datetime_series[5:])
Expand Down Expand Up @@ -82,7 +83,12 @@ def test_append_dataframe_raises(self):
with pytest.raises(TypeError, match=msg):
df.A.append([df])

def test_append_raises_future_warning(self):
with tm.assert_produces_warning(FutureWarning):
Series([1, 2]).append(Series([3, 4]))


@pytest.mark.filterwarnings("ignore:.*append method is deprecated.*:FutureWarning")
class TestSeriesAppendWithDatetimeIndex:
def test_append(self):
rng = date_range("5/8/2012 1:45", periods=10, freq="5T")
Expand All @@ -107,6 +113,7 @@ def test_append(self):
rng2 = rng.copy()
rng1.name = "foo"
rng2.name = "bar"

assert rng1.append(rng1).name == "foo"
assert rng1.append(rng2).name is None

Expand Down