Skip to content

Instantly share code, notes, and snippets.

View keivanipchihagh's full-sized avatar
:octocat:
Working from bed recently

Keivan Ipchi Hagh keivanipchihagh

:octocat:
Working from bed recently
View GitHub Profile
def upsert_df(self, df: pd.DataFrame, table_name: str) -> None:
"""
Implements the equivalent of pd.DataFrame.to_sql(..., if_exists='update') (which does not exist). Creates or updates the db records based on the dataframe records.
Parameters:
df (pd.DataFrame): Dataframe to upsert (NOTE: Primary keys of the destination 'table_name' must be equal to dataframe index and not present in the dataframe columns)
table_name (str): Table name
client (PostgresClient): Database client
Returns:
None
"""