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

pandas remove seconds from datetime index

我有一个名为 '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

我只是想去掉秒来得到这个

                       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

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

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