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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:08:25  来源:igfitidea点击:

How do I change time format in pandas?

pythonpandasdatetime

提问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 Tas separator and Zas 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 formatonly:

我认为你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