我有以下代码:
from pandas.io import sql
import sqlite3
conn = sqlite3.connect('breakDownRecs.sqlite')
query = "SELECT * FROM indRecs"
df = sql.read_sql(query, con=conn) #parse_dates=['DateDayId'])
print(df)
我得到(正确)类似的东西:
DateID Usr EventTypeM DateDayID DateMonthID
0 20151007221243 Noelle LVL 20151007 201510
1 20151007225607 Morak QST 20151007 201510
2 20151007225621 Austinpower QST 20151007 201510
...
问题是如何解析它以便将DateDayID列作为日期,以便以后编入索引?如果我提供:
df = sql.read_sql(query, con=conn, parse_dates=['DateDayID'])
它给了我
DateID Usr EventTypeM DateDayID DateMonthID
0 20151007221243 Noelle LVL NaT 201510
1 20151007225607 Morak QST NaT 201510
...
谢谢!
答案 0 :(得分:1)
我认为您需要to_datetime
参数format
:
df['DateID'] = pd.to_datetime(df.DateID, format='%Y%m%d%H%M%S')
print df
DateID Usr EventTypeM DateDayID DateMonthID
0 2015-10-07 22:12:43 Noelle LVL NaT 201510
1 2015-10-07 22:56:07 Morak QST NaT 201510
答案 1 :(得分:0)
尝试:
import pandas as pd
df.loc[:, 'DateID'] = pd.to_datetime(df.DateID)
答案 2 :(得分:0)
df = sql.read_sql(query, con=conn, parse_dates=['DateID'])
或
df = sql.read_sql(query, con=conn, parse_dates={'DateID': {'format': '%Y-%m-%d'}})