Skip to content Skip to sidebar Skip to footer

How To Store A Pandas Dataframe With Datetime.timedelta Type Data Objects Into A Postgresql D/b Using Sqlalchemy?

I would like to store a pandas dataframe containing columns of type timedelta64 in a Postgresql database using sqlalchemy. Reading the documentation (https://docs.sqlalchemy.org/en

Solution 1:

Timedelta is converted into integer values. Refer to https://github.com/pandas-dev/pandas/blob/master/pandas/io/sql.py#L880-L883

A workaround would be converting the timedelta in string format, before saving it to db.

def a_func(val):
    return str(val)

my_df['delay'] = my_df['delay'].apply(a_func)

or

my_df['delay'] = my_df['delay'].astype(str)

Post a Comment for "How To Store A Pandas Dataframe With Datetime.timedelta Type Data Objects Into A Postgresql D/b Using Sqlalchemy?"