pandas 如何更改熊猫的时间格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45418596/
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
How do I change time format in pandas?
提问by kamome
I am trying to change the format of a column (serie) in pandas.
我正在尝试更改 Pandas 中列(系列)的格式。
The column is datetime object in the following format: 2017-06-14 08:00:00
该列是以下格式的日期时间对象: 2017-06-14 08:00:00
I would like to format the column as: 2017-06-14T08:00Z
. Including T
as separator and Z
as UTC time
我想将列的格式设置为:2017-06-14T08:00Z
。包括T
作为分隔符和Z
作为 UTC 时间
df["time(UTC)"].dt.strftime("%Y:%m%:%dT%H:%MZ")`
But is doesn't work.
但这是行不通的。
How can I do that?
我怎样才能做到这一点?
回答by jezrael
I think you need change format
only:
我认为你format
只需要改变:
df=pd.DataFrame({'time(UTC)':pd.to_datetime(['2017-06-14 08:00:00','2017-06-14 08:00:00'])})
df['new'] = df["time(UTC)"].dt.strftime("%Y-%m-%dT%H:%MZ")
print (df)
time(UTC) new
0 2017-06-14 08:00:00 2017-06-14T08:00Z
1 2017-06-14 08:00:00 2017-06-14T08:00Z