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

I read data_frame directly from the 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) 

It works great when retrieving data. But I would like to analyze one of the columns as datetime64 , akin to what you can do when reading from a CSV file, for example:

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

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

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

+6
python sql pandas datetime
Mar 05 '13 at 18:44
source share
3 answers

This question is very old. pandas 0.10 is also very old. In the latest version of pandas 0.16, the read_frame method was deprived in favor of read_sql. Despite this, the documentation says that just like the read_csv function, it takes the parse_dates Pandas 0.16 read_frame argument

It seems that the parse_dates argument appeared in 0.14, while read_frame was stripped. The read_sql function is apparently a renaming of read_frame, so just upgrade your pandas version to version 0.14 or higher and renaming your function will give you access to this argument. Here is the document for the read_sql function: Pandas 0.16 read_sql

+1
Apr 15 '15 at 7:58
source share
 df = pandas.io.sql.read_frame('sql_query', index=['date_column_name'], con=cnx) 

where date_column_name is the name of the column in the database containing the date elements. sql_query should be of the form select date_column_name, data_column_name from ...

Pandas (starting at 0.13+) will automatically parse it to the date format if it looks like a date string.

 In [34]: df.index Out[34]: <class 'pandas.tseries.index.DatetimeIndex'> 
0
Jan 21 '14 at 8:13
source share
 data_frame["column"] = pandas.to_datetime(data_frame["column"]) 

should work by default, but if you cannot specify parameters. See doc .

0
Apr 10 '14 at 16:28
source share



All Articles