作者:大佬銘銘銘銘銘 | 来源:互联网 | 2023-10-10 18:08
当我从SQLServer将“Date”变量拉入Python/Pandas时,它将作为“对象”出现.我已经安装并尝试了几个驱动程序(代码中显示的注释驱动程序),每次都有相同的结果:
当我从SQL Server将“Date”变量拉入Python / Pandas时,它将作为“对象”出现.我已经安装并尝试了几个驱动程序(代码中显示的注释驱动程序),每次都有相同的结果:
import pandas as pd
import pyodbc
conn_str = (
r'Driver={SQL Server Native Client 11.0};'
# r'Driver={SQL Server Native Client 10.0};'
# r'Driver={ODBC Driver 11 for SQL Server};'
# r'Driver={ODBC Driver 13 for SQL Server};'
# r'Driver={SQL Server};'
r'Server=MyServer;'
r'Database=MyDB;'
r'Trusted_COnnection=yes;'
)
cnxn = pyodbc.connect(conn_str)
sql = (
"Select cast('2017-08-19' as date) [DateVar]"
", cast('2017-08-19' as datetime) [DateTimeVar]"
", cast('2017-08-19' as datetime2) [DateTime2Var]"
)
d2 = pd.read_sql(sql,cnxn)
cnxn.close()
print(d2.dtypes)
返回的结果是:
DateVar object
DateTimeVar datetime64[ns]
DateTime2Var datetime64[ns]
dtype: object
我希望DateVar成为日期时间.任何想法为什么会这样?
和这家伙一样的问题:
pyodbc returns SQL Server DATE fields as strings
但他的修复是使用我安装的{SQL Server Native Client 10.0}并且不适合我.
我正在连接的SQL Server版本是:
Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64)
Oct 20 2015 15:36:27
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1)
更新
1 GT;
基于Max的输入,尝试了sqlalchemy,但没有运气,这仍然给了我一个字符串:
import sqlalchemy as sa
engine = sa.create_engine('mssql+pyodbc://MyDatabase/MyDB?driver=SQL+Server+Native+Client+10.0')
d2 = pd.read_sql(sql, engine)
2 – ;
基于Flipper的Q,只使用Pyodbc游标完成此操作,看起来使用Native Client 11.0时光标中返回了正确的日期数据类型:
(('DateVar', datetime.date, None, 10, 10, 0, True),
('DateTimeVar', datetime.datetime, None, 23, 23, 3, True),
('DateTime2Var', datetime.datetime, None, 27, 27, 7, True))
这表明在加载到数据帧时,问题在于Pandas处理dtype datetime.date.
解决方法:
使用pandas.read_sql的parse_dates参数指定在数据帧加载时将DateVar列值显式转换为datetime.
更新了原始代码段:
...
d2 = pd.read_sql(sql=sql, con=cnxn, # explicitly convert DATE type to datetime object parse_dates=["DateVar"])
cnxn.close()
print(d2.dtypes)
返回
DateVar datetime64[ns]
DateTimeVar datetime64[ns]
DateTime2Var datetime64[ns]
dtype: object
在Windows上使用pyodbc 4.0.17,pandas 0.20.3和SQL Server 2014进行了测试.