作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 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()
我得到以下输出
如何将日期列作为索引。
最佳答案
尝试:
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/
我是一名优秀的程序员,十分优秀!