SQL Server DATE作为字符串检索到pandas中

时间:2017-08-19 16:31:20

标签: python sql-server pandas pyodbc

当我从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 <X64> (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

2 个答案:

答案 0 :(得分:3)

使用pandas.read_sqlparse_dates参数指定DateVar列值在数据帧加载时显式转换为日期时间。

更新了原始代码段:

...
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进行测试。

答案 1 :(得分:0)

尝试使用SQLAlchemy,如下所示:

from sqlalchemy import create_engine

engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")