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

2024-04-26 15:13:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从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'])panda.series而不是{}?我不知道原因。在我的SQL server中,日期的格式也是“date”。在

你能建议我怎样把这个系列转换成时间序列吗?在

^{pr2}$

我得到以下输出enter image description here

如何将日期列作为索引。在


Tags: 数据服务器iddfsqldateasmysql
1条回答
网友
1楼 · 发布于 2024-04-26 15:13:00

尝试:

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

或者:

^{pr2}$

如果您的datetime格式异常:

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

相关问题 更多 >