pandas 使用pandas.io.sql.read_frame,我可以像read_csv那样解析日期吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15231646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:41:17  来源:igfitidea点击:

Using pandas.io.sql.read_frame, can I parse_dates, as in read_csv?

pythonsqldatetimepandas

提问by random.me

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

我正在data_frame使用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)

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.:

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

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

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

但是没有parse_dates标志read_frame。推荐什么替代方法?

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

同样的问题适用于index_colin read_csv,它指示哪个列。应该是索引。是否有推荐的方法来使用 read_frame 执行此操作?

回答by firelynx

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

这个问题现在已经很老了。pandas 0.10 也很老了。在最新版本的 Pandas 0.16 中,read_frame 方法已被弃用,取而代之的是 read_sql。即便如此,文档说就像 read_csv 函数一样,它需要一个 parse_dates 参数Pandas 0.16 read_frame

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

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

回答by Kracit

data_frame["column"] = pandas.to_datetime(data_frame["column"])

should work by default but if not you can specify options. See the doc.

默认情况下应该工作,但如果不是,您可以指定选项。请参阅文档

回答by oDDsKooL

df = pandas.io.sql.read_frame('sql_query', index=['date_column_name'], con=cnx)

where date_column_nameis the name of the column in the database that contains date elements. sql_queryshould then be of the form select date_column_name, data_column_name from ...

其中date_column_name是数据库中包含日期元素的列的名称。sql_query那么应该是这样的形式select date_column_name, data_column_name from ...

Pandas (as of 0.13+) will then automatically parse it to a date format if it resembles a date string.

如果它类似于日期字符串,Pandas(从 0.13+ 开始)将自动将其解析为日期格式。

In [34]: df.index
Out[34]: 
    <class 'pandas.tseries.index.DatetimeIndex'>