如何使用ExecuteMy将NaT类型插入MySQL datetime列

2024-06-25 06:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到df看起来像这样

uid | name | txn   | date
 1  |  a   | NaN   | NaT
 2  |  z   | 970.0 | 2016-04-30 17:00:00 

当我从MySQLdb游标使用executemany时,它返回

Executemany error while connecting to MySQL (1292, "Incorrect datetime value: 'NaT' for column date at row 1")

因此,我尝试使用df.replace({pd.Nat: None}, inplace=True)将NaT转换为None,现在它返回

Executemany error while connecting to MySQL (1366, "Incorrect integer value: 'None' for column 'txn' at row 1")

因此,我需要使用这个df.replace({pd.NaT: None, np.NaN: None}, inplace=True),但是仍然得到

Executemany error while connecting to MySQL (1292, "Incorrect datetime value: 'None' for column 'date' at row 1")

鉴于df.replace behavior的知识

如何解决NaT/None不正确的日期时间值问题


Tags: tononedffordatevaluemysqlcolumn
1条回答
网友
1楼 · 发布于 2024-06-25 06:35:59

我一直在用谷歌搜索同一个问题的解决方案

Executemany error while connecting to MySQL (1292, "Incorrect datetime value: 'NaT' for column date at row 1")

经过几个小时的研究,我将datetime转换为字符串类型,然后运行replace函数

df['date'] = df['date'].astype('str')
df.replace({'NaT': None}, inplace=True)

MySQL接受了我的insert语句。希望这对你也有用。还要检查数据库结构,确保“日期”列允许空值

相关问题 更多 >