pandas 熊猫从日期时间索引中删除秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39805961/
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 02:07:12  来源:igfitidea点击:

pandas remove seconds from datetime index

pythonpandasindexingseconds

提问by Runner Bean

I have a pandas dataFrame called 'df' as follows

我有一个名为“df”的Pandas数据框,如下所示

                       value    
2015-09-27 03:58:30    1.0  
2015-09-27 03:59:30    1.0  
2015-09-27 04:00:30    1.0  
2015-09-27 04:01:30    1.0

I just want to strip out the seconds to get this

我只想去掉秒来得到这个

                       value    
2015-09-27 03:58:00    1.0  
2015-09-27 03:59:00    1.0  
2015-09-27 04:00:00    1.0  
2015-09-27 04:01:00    1.0

How can i do this?

我怎样才能做到这一点?

ive tried things like

我试过这样的事情

df.index.to_series().apply(datetime.replace(second=0, microsecond=0))

but i always get errors

但我总是出错

TypeError: descriptor 'replace' of 'datetime.datetime' object needs an argument

回答by Nickil Maveli

You could use datetime.replaceto alter the second's attribute as shown:

您可以使用datetime.replace更改第二个的属性,如下所示:

df.index = df.index.map(lambda x: x.replace(second=0))

Image

图片