如何将熊猫系列转换为时间序列?

时间:2014-07-12 19:53:16

标签: python mysql pandas

我正在尝试从MYSQL服务器中提取数据并从中生成数据帧。我使用的SQL查询是

sql="""SELECT dp.Date, dp.Open , dp.High, dp.Low, dp.Close, dp.Volume, dp.Adj 
     FROM tickers AS tick
     INNER JOIN daily_price AS dp
     ON dp.ticker_id = tick.id
     WHERE tick.ticker = '%s'
     ORDER BY dp.Date ASC;"""%(ticker)
goog = psql.frame_query(sql, con=con, index_col='Date')

这工作得很好但是当我使用函数df=obtain_df(ticker)时(obtain_df只是获取dataframe的函数)并使用type(df['High'])它{{1}而不是panda.series?我不知道原因。在我的SQL服务器中,日期的格式为' DATE'。

您能否建议我如何将系列转换为时间序列?

timeseries

我得到以下输出enter image description here

如何将日期列设为索引。

1 个答案:

答案 0 :(得分:0)

尝试:

 df['Date'] = pd.DatetimeIndex(df['Date'])

或:

 df['Date'] = pd.to_datetime(df['Date'])

如果datetime格式不正常:

 df['Date'] = pd.to_datetime(df['Date'], format="%d%m%Y")
相关问题