pandas从datetime索引中删除秒数

时间:2016-10-01 11:19:45

标签: python pandas indexing seconds

我有一个名为'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: descriptor 'replace' of 'datetime.datetime' object needs an argument

1 个答案:

答案 0 :(得分:14)

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

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

Image