pandas remove seconds from datetime index

Runner Bean picture Runner Bean · Oct 1, 2016 · Viewed 11k times · Source

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

Answer

Nickil Maveli picture Nickil Maveli · Oct 1, 2016

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

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

Image