postgresql - 如何使用 pandas sqlalchemy 和 psycopg2 处理 NaT

标签 postgresql python-3.x pandas sqlalchemy psycopg2

我有一个带有 NaT 的数据框,所以它给了我一个 DataError: (psycopg2.DataError) invalid input syntax for type timestamp: "NaT": 当我尝试将值插入 postgres 时分贝

数据框

from sqlalchemy import MetaData
from sqlalchemy.dialects.postgresql import insert
import pandas as pd

tst_df = pd.DataFrame({'colA':['a','b','c','a','z', 'q'],
                  'colB': pd.date_range(end=datetime.datetime.now() , periods=6),
                  'colC' : ['a1','b2','c3','a4','z5', 'q6']})
tst_df.loc[5, 'colB'] = pd.NaT



insrt_vals = tst_df.to_dict(orient='records')
engine = sqlalchemy.create_engine("postgresql://user:password@localhost/postgres")
connect = engine.connect()
meta = MetaData(bind=engine)
meta.reflect(bind=engine)
table = meta.tables['tstbl']
insrt_stmnt = insert(table).values(insrt_vals)

do_nothing_stmt  = insrt_stmnt.on_conflict_do_nothing(index_elements=['colA','colB'])

产生错误的代码

results = engine.execute(do_nothing_stmt)

DataError: (psycopg2.DataError) invalid input syntax for type timestamp: "NaT"
LINE 1: ...6-12-18T09:54:05.046965'::timestamp, 'z5'), ('q', 'NaT'::tim...

提到了一种可能性 here是用 None 替换 NaT,但正如前一位作者所说,这似乎有点老套。

sqlachemy  1.1.4
pandas  0.19.1
psycopg2 2.6.2 (dt dec pq3 ext lo64)

最佳答案

您是否尝试使用 Pandas to_sql方法?

它适用于 MySQL 数据库(我想它也适用于 PostgreSQL):

In [50]: tst_df
Out[50]:
  colA                       colB colC
0    a 2016-12-14 19:11:36.045455   a1
1    b 2016-12-15 19:11:36.045455   b2
2    c 2016-12-16 19:11:36.045455   c3
3    a 2016-12-17 19:11:36.045455   a4
4    z 2016-12-18 19:11:36.045455   z5
5    q                        NaT   q6

In [51]: import pymysql
    ...: import sqlalchemy as sa
    ...:

In [52]:

In [52]: db_connection = 'mysql+pymysql://user:password@mysqlhost/db_name'
    ...:

In [53]: engine = sa.create_engine(db_connection)
    ...: conn = engine.connect()
    ...:

In [54]: tst_df.to_sql('zzz', conn, if_exists='replace', index=False)

在 MySQL 方面:

mysql> select * from zzz;
+------+---------------------+------+
| colA | colB                | colC |
+------+---------------------+------+
| a    | 2016-12-14 19:11:36 | a1   |
| b    | 2016-12-15 19:11:36 | b2   |
| c    | 2016-12-16 19:11:36 | c3   |
| a    | 2016-12-17 19:11:36 | a4   |
| z    | 2016-12-18 19:11:36 | z5   |
| q    | NULL                | q6   |
+------+---------------------+------+
6 rows in set (0.00 sec)

很遗憾,我没有用于测试的 PostgreSQL

关于postgresql - 如何使用 pandas sqlalchemy 和 psycopg2 处理 NaT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41227118/

相关文章:

python - 如何将函数应用于 pandas 数据框?

python - 如何获得星号 (*) 而不是数字 0 的结果?

javascript - 如何在动态导入功能中导入Sequelize?

database - 如何删除除 postgres 中的少数数据库之外的所有数据库

sql - 添加到 Postgres 中的 json 列中的数组

python - 为遗传算法中的适应度比例选择(轮盘赌)生成概率列表

python - 运行 PyQt 应用程序时出现空白表单

python - 优化将列表列拆分为单独的列

java - 如何在 pl/pgsql 中创建返回 refcursor 和totalRow 的函数/过程?

python - 在 Python 中获取图像对象大小(以字节为单位)- Azure 中的 BadRequest 错误