【发布时间】:2016-04-04 20:10:36
【问题描述】:
我正在尝试将我的 pandas 数据框中的日期时间值导入到我的 TSQL 表的日期时间列中。
问题是,如果我的任何列包含 NULL 值“NaT”,我会收到错误
见下文:
import pyodbc
import pandas as pd
cnxn = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
host=server,database=dbname,
trusted_connection=tcon,
user=uname,password=pword)
cursor = cnxn.cursor()
df.head()
Date_1 Date_2
1 2015-07-01 10:53:16 2015-07-01 00:13:09
2 2015-07-03 10:31:16 2015-07-01 16:39:40
3 2015-06-26 14:39:19 2015-06-24 13:56:17
for index, row in df.iterrows():
cursor.execute("""
INSERT INTO Table(
Date1,Date2)"""
"""VALUES (?,?)""",
row['Date1'],row['Date2'])
cnxn.commit()
DataError: ('22007', '[22007] [Microsoft][SQL Server Native Client 10.0]无效的日期格式(0) (SQLExecDirectW)')
这与我的日期时间列中的 Null 值有关。当我在我的数据框中查看空值时,它们显示为 NaT。
我不知道如何向我的 TSQL 表导入任何内容。
【问题讨论】: