使用pandas.io.sql.read_frame,我可以在parse_dates,如在read_csv? [英] Using pandas.io.sql.read_frame, can I parse_dates, as in read_csv?

查看:673
本文介绍了使用pandas.io.sql.read_frame,我可以在parse_dates,如在read_csv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pandas.io.sql.read_frame 直接从数据库中读取 data_frame

I am reading a data_frame directly from a database using pandas.io.sql.read_frame:

cnx = pandas.io.sql.connect(host='srv',user='me',password='pw',database='db')
df = pandas.io.sql.read_frame('sql_query',cnx)

它在检索数据时工作得很好。但是,我想将一列作为一个 datetime64 解析,类似于从CSV文件读取时可以执行的操作,例如:

It works nicely in retrieving the data. But I would like to parse one of the columns as a datetime64, akin to what can be done when reading from a CSV file, e.g.:

df2 = pandas.io.read_csv(csv_file, parse_dates=[0])

但是 read_frame 中没有 parse_dates 标志。推荐使用哪种替代方法?

But there is no parse_dates flag for read_frame. What alternative approach is recommended?

同样的问题适用于read_csv中的 index_col ,它指示哪个col。应该是索引。有没有推荐使用read_frame做的方法?

The same question applies to the index_col in read_csv, which indicates which col. should be the index. Is there a recommended way to do this with read_frame?

推荐答案

现在这个问题很老了。大熊猫0.10也很老。
在最新版本的大熊猫0.16中,read_frame方法已经被取消了赞成read_sql。
即使如此,文档就像read_csv函数一样,它需要一个parse_dates参数 Pandas 0.16 read_frame

This question is very old by now. pandas 0.10 is very old as well. In the newest version of pandas 0.16, the read_frame method has been depricated in favour of the read_sql. Even so, the documentation says that just like the read_csv function, it takes a parse_dates argument Pandas 0.16 read_frame

似乎parse_dates参数出现在0.14,同时read_frame被删除。
read_sql函数似乎是read_frame的重命名,因此只需将大熊猫版本更新到0.14或更高版本,并重命名您的函数即可访问此参数。
这是read_sql函数的文档: Pandas 0.16 read_sql

It seems the parse_dates argument appeared in 0.14, at the same time as read_frame was depricated. The read_sql function seems to be a rename of the read_frame, so just updating your pandas version to 0.14 or higher and renaming your function will give you access to this argument. Here is the doc for the read_sql function: Pandas 0.16 read_sql

这篇关于使用pandas.io.sql.read_frame,我可以在parse_dates,如在read_csv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆