将 Pandas DataFrame 索引转换为时间戳格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42399552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:02:30 来源:igfitidea点击:
Convert pandas DataFrame index to Timestamp format
提问by rsp
I created a dataframe. Running some commands on the iPython I get the following results:
我创建了一个数据框。在 iPython 上运行一些命令我得到以下结果:
In [5]: df.tail()
Out[5]:
open high low close volume
date
2017-02-22 15:00 1.05131 1.05137 1.05074 1.05075 543
2017-02-22 15:30 1.05074 1.05165 1.05072 1.05139 506
2017-02-22 16:00 1.05137 1.05193 1.05121 1.05141 488
2017-02-22 16:30 1.05144 1.05205 1.05056 1.05065 747
2017-02-22 17:00 1.05061 1.05167 1.04988 1.04997 753
In [6]: df.index[0]
Out[6]: '2011-11-21 14:30'
In [7]: type(df.index[0])
Out[7]: str
As you see, the index is in str format but I need it to be in Timestamp format. Is it possible?
如您所见,索引采用 str 格式,但我需要它采用时间戳格式。是否可以?
回答by Vaishali
Try this
尝试这个
df.index = pd.to_datetime(df.index)