pandas remove seconds from datetime index

2020-03-14 19:18发布

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条回答
家丑人穷心不美
2楼-- · 2020-03-14 19:53

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

查看更多
登录 后发表回答