我想使用sqlalchemy在Postgresql数据库中存储包含timedelta64类型列的熊猫数据框。阅读文档(https://docs.sqlalchemy.org/en/latest/core/type_basics.html)我希望python'timedelta'数据类型可以映射到postgresql'interval'数据类型,但是我不知道该怎么做。我尝试了以下代码:
import sqlalchemy as sa
import pandas as pd
from datetime import timedelta
engine = sa.create_engine('postgresql+psycopg2://postgres:password@floris/floris')
my_df = pd.DataFrame(data=[ timedelta(days=1), timedelta(days=2), timedelta(days=3)], index=range(0,3), columns=['delay'])
my_df.to_sql('my_table', con=engine, dtype={'delay': sa.types.Interval})
我遇到以下错误:
psycopg2.ProgrammingError: column "delay" is of type interval but expression is of type bigint
LINE 1: INSERT INTO my_table (index, delay) VALUES (0, 8640000000000...
^
HINT: You will need to rewrite or cast the expression.
似乎sqlalchemy并未保留timedelta数据类型,而是将其转换为bigint。该如何解决?
答案 0 :(得分:0)
Timedelta转换为整数值。参考 https://github.com/pandas-dev/pandas/blob/master/pandas/io/sql.py#L880-L883
一种解决方法是将timedelta转换为字符串格式,然后再将其保存到db。
def a_func(val):
return str(val)
my_df['delay'] = my_df['delay'].apply(a_func)
或
my_df['delay'] = my_df['delay'].astype(str)