gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:34:32 26 4
gpt4 key购买 nike

我正在尝试从 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 而不是 timeseries?我不知道这是为什么。在我的 SQL 服务器中,日期的格式也是“DATE”。

你能建议我如何将序列转换为时间序列吗?

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

da.set_index('Date')

print da.head()

我得到以下输出 enter image description here

如何将日期列作为索引。

最佳答案

尝试:

 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")

关于python - 如何将 Pandas 系列转换为时间系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com