Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas remove seconds from datetime index

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
like image 755
Runner Bean Avatar asked Oct 01 '16 11:10

Runner Bean


People also ask

How do I remove seconds from datetime?

Solution 1 Using different methods on the DateTime, like . Today give the date part, but the other parts of it are zero. This is by design. If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.

How do I remove the seconds from a datetime column in Python?

If you just want strings, you could remove the trailing seconds with a regex ':\d\d$' .

How do I get rid of hours minutes and seconds in Python?

To remove the time from a datetime object in Python, convert the datetime to a date using date(). You can also use strftime() to create a string from a datetime object without the time.

Can only use .DT accessor with Datetimelike values in Python?

dt accessor with datetimelike values error occurs while converting string to datetime format in the specific situation. These specific situations are if are converting multiple string values into datetime format values (complete pandas dataframe column) and some of the values are having errors in conversion.


1 Answers

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

like image 69
Nickil Maveli Avatar answered Oct 06 '22 00:10

Nickil Maveli