pandas remove seconds from datetime index

2020-03-14 19:47发布

问题:

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

回答1:

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

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