pandas从datetime索引中删除秒数 [英] pandas remove seconds from datetime index

查看:1623
本文介绍了pandas从datetime索引中删除秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为'df'的pandas dataFrame,如下所示

  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

我只想消除秒数来获得这个

  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

我该怎么办?



我尝过类似的事情

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

但我总是得到错误

  TypeError:'datetime.datetime'描述符'replace'对象需要一个参数


解决方案

您可以使用


I have a pandas dataFrame called 'df' as follows

                       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

解决方案

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

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

这篇关于pandas从datetime索引中删除秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆