Registry / database / pangres

pangres

JSON →
library4.2.1pypypi✓ verified 85d ago

Pangres is a Python library that provides efficient upsert (insert/update) functionality for pandas DataFrames into SQL databases (PostgreSQL, MySQL, SQLite) via SQLAlchemy. It supports both synchronous and asynchronous operations, with automatic table creation and schema inference. The current version is 4.2.1, with monthly or irregular releases.

pip install pangres
INSTALL
IMPORT
SIG · PANGRES
P
pangres
databasepythonv4.2.1
Install
10.8s avg
Import
2076ms
Disk
195MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 2.152s · 195.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 10.8s · import 2.000s · 186MB
195MB installed
● package 195MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

upsert
from pangres import upsert
import pangres
While 'import pangres; pangres.upsert(...)' works, direct import is preferred.
aupsert
from pangres import aupsert
Async upsert function introduced in v4.1.

Inserts or updates rows in PostgreSQL using a DataFrame with a primary key index. The engine can be replaced with a connection for transaction control.

import pandas as pd from sqlalchemy import create_engine from pangres import upsert engine = create_engine('postgresql://user:pass@localhost/db') df = pd.DataFrame({'id': [1, 2], 'value': ['a', 'b']}).set_index('id') upsert(con=engine, df=df, table_name='my_table', if_row_exists='update')
Debug
Known issues
breakingIn v4.0, the first argument 'engine' was renamed to 'con', and now accepts both engines and connections. Old code using 'engine=' keyword will break.
fix
Use 'con=' instead of 'engine='.
affects: <4.0
breakingIn v4.0, the 'chunksize' parameter default changed from 10000 to None (insert all rows at once). If you relied on the old default, your upserts may now attempt huge transactions.
fix
Explicitly set chunksize=10000 to restore old behavior.
affects: <4.0
gotchaWhen using async (aupsert), the input 'con' must be an async SQLAlchemy engine or connection, not a sync one. Mixing sync/async will raise an error.
fix
Ensure you create an async engine with create_async_engine from sqlalchemy.ext.asyncio.
affects: >=4.1
deprecatedSupport for Python 3.12 was added in v4.2.0 with a new dependency 'packaging'. Users on Python 3.12 must upgrade to v4.2.0+.
fix
Upgrade pangres to >=4.2.0.
affects: <4.2.0
Errors
Common errors & fixes
AttributeError: module 'pangres' has no attribute 'upsert'
After 'import pangres', the upsert function is not directly accessible as a module attribute in older versions or due to missing exports.
fix
Use 'from pangres import upsert' or upgrade to v4.2.1 where exports are fixed.
sqlalchemy.exc.InvalidRequestError: A value is required for a bound parameter in the WHERE clause
Pangres requires the DataFrame index to be named (as a column in the database). An unnamed index or MultiIndex with duplicate level names causes this.
fix
Ensure the DataFrame index has a name (e.g., df.index.name = 'id'). For MultiIndex, ensure all level names are unique and match the database primary key columns.
Upgrade
Version history
4.2.1latest on PyPI · released Nov 5, 2023
Audit
Dependencies
pandasrequiredUpsert operations require DataFrames
sqlalchemyrequiredDatabase connection and operations
alembicrequiredRequired for SQLAlchemy >= 2.0 compatibility (>=1.7.2)
packagingrequiredDependency added in v4.2.0 for version parsing
psycopg2optionalRecommended for PostgreSQL connections
asyncpgoptionalRequired for async PostgreSQL support
aiosqliteoptionalRequired for async SQLite support
aiomysqloptionalRequired for async MySQL support
Agent activity
2 hits · last 30 days
node
2
Resources
pangres — pip install pangres · libregistry