Skip to content

Commit dd1852d

Browse files
shanraljreback
authored andcommitted
DOC: add warning to append about inefficiency (#17017)
1 parent 5a02449 commit dd1852d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

pandas/core/frame.py

+32
Original file line numberDiff line numberDiff line change
@@ -4715,6 +4715,11 @@ def append(self, other, ignore_index=False, verify_integrity=False):
47154715
the DataFrame's index, the order of the columns in the resulting
47164716
DataFrame will be unchanged.
47174717
4718+
Iteratively appending rows to a DataFrame can be more computationally
4719+
intensive than a single concatenate. A better solution is to append
4720+
those rows to a list and then concatenate the list with the original
4721+
DataFrame all at once.
4722+
47184723
See also
47194724
--------
47204725
pandas.concat : General function to concatenate DataFrame, Series
@@ -4745,6 +4750,33 @@ def append(self, other, ignore_index=False, verify_integrity=False):
47454750
2 5 6
47464751
3 7 8
47474752
4753+
The following, while not recommended methods for generating DataFrames,
4754+
show two ways to generate a DataFrame from multiple data sources.
4755+
4756+
Less efficient:
4757+
4758+
>>> df = pd.DataFrame(columns=['A'])
4759+
>>> for i in range(5):
4760+
... df = df.append({'A'}: i}, ignore_index=True)
4761+
>>> df
4762+
A
4763+
0 0
4764+
1 1
4765+
2 2
4766+
3 3
4767+
4 4
4768+
4769+
More efficient:
4770+
4771+
>>> pd.concat([pd.DataFrame([i], columns=['A']) for i in range(5)],
4772+
... ignore_index=True)
4773+
A
4774+
0 0
4775+
1 1
4776+
2 2
4777+
3 3
4778+
4 4
4779+
47484780
"""
47494781
if isinstance(other, (Series, dict)):
47504782
if isinstance(other, dict):

pandas/core/series.py

+12
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,18 @@ def append(self, to_append, ignore_index=False, verify_integrity=False):
15631563
verify_integrity : boolean, default False
15641564
If True, raise Exception on creating index with duplicates
15651565
1566+
Notes
1567+
-----
1568+
Iteratively appending to a Series can be more computationally intensive
1569+
than a single concatenate. A better solution is to append values to a
1570+
list and then concatenate the list with the original Series all at
1571+
once.
1572+
1573+
See also
1574+
--------
1575+
pandas.concat : General function to concatenate DataFrame, Series
1576+
or Panel objects
1577+
15661578
Returns
15671579
-------
15681580
appended : Series

0 commit comments

Comments
 (0)