Skip to main content

pandas-appender

build coverage Apache License 2.0

Have you ever wanted to append a bunch of rows to a Pandas DataFrame? Turns out that it's extremely inefficient to do! For a large dataframe, you're supposed to make multiple dataframes and pd.concat() them instead.

Also, Pandas deprecated dataframe.append() in version 1.4 and intends to remove it in 2.0.

So... helper function? Pandas doesn't have one. Roll your own? Ugh. OK then: here's that helper function. It can append around 1 million very small rows per cpu-second. It has a modest additional memory usage of around 5 megabytes, dynamically growing with the number of rows appended.

Install

pip install pandas-appender

Usage

from pandas_appender import DF_Appender

dfa = DF_Appender(ignore_index=True)  # note that ignore_index moves to the init
for i in range(1_000_000):
    dfa = dfa.append({'i': i})

df = dfa.finalize()  # must call .finalize() before you can use the results

Type hints and category detection

Using narrower types and categories can often dramatically reduce the size of a DataFrame. There are two ways to do this in pandas-appender. One is to append to an existing dataframe:

dfa = DF_Appender(df, ignore_index=True)

and the second is to pass in a dtypes= argument:

dfa = DF_Appender(ignore_index=True, dtypes=another_dataframe.dtypes)

pandas-appender also offers a way to infer which columns would be smaller if they were categories. This code will either analyze an existing dataframe that you're appending to:

dfa = DF_Appender(df, ignore_index=True, infer_categories=True)

or it will analyze the first chunk of appended lines:

dfa = DF_Appender(ignore_index=True, infer_categories=True)

These inferred categories will override existing types or a dtypes= argument.

Incompatibilities with pandas.DataFrame.append()

DF_Appender must be finalized before use

  • Pandas: df_new = df.append() # df_new is a dataframe
  • DF_Appender: dfa_new = dfa.append() # must do df = dfa.finalize() to get a DataFrame

pandas.DataFame.append is idempotent, DF_Appender is not

  • Pandas: df_new = df.append() # df is not changed
  • DF_Appender: dfa_new = dfa.append() # modifies dfa, and dfa_new == dfa

pandas.DataFrame.append will promote types, while DF_Appender is strict

  • Pandas: append 0.1 to an integer column, and the column will be promoted to float
  • DF_Appender: when initialized with dtypes= or an existing DataFrame, appending 0.1 to an integer column causes 0.1 to be cast to an integer, i.e. 0.

Metadata

Release files for pandas-appender 0.9.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pandas-appender 0.9.9
File Size Uploaded
pandas_appender-0.9.9.tar.gz 13.7 kB Details

Release files / pandas_appender-0.9.9.tar.gz

Download URL pandas_appender-0.9.9.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f14da527a327702ea2b90a2e0cd9f587661a43e580e441823fd9deca11e8a383
BLAKE2b-256 checksum
How to use checksums
90d881db9067261f460b67d9c9154ee48b84846099fedc5e18a2da77f029f52b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.9.9 This release

1 release file

0.9.8.4

1 release file

0.9.8.1

1 release file

0.9.8

1 release file

0.9.7

1 release file

0.9.6

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page