Skip to content

Instantly share code, notes, and snippets.

View pascaloberle's full-sized avatar

Pascal Oberle pascaloberle

View GitHub Profile
@pedrovgp
pedrovgp / upsert_df.py
Last active February 11, 2024 20:44
Allow upserting a pandas dataframe to a postgres table (equivalent to df.to_sql(..., if_exists='update')
# Upsert function for pandas to_sql with postgres
# https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/8702291#8702291
# https://www.postgresql.org/docs/devel/sql-insert.html#SQL-ON-CONFLICT
import pandas as pd
import sqlalchemy
import uuid
import os
def upsert_df(df: pd.DataFrame, table_name: str, engine: sqlalchemy.engine.Engine):