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
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.
If you just want strings, you could remove the trailing seconds with a regex ':\d\d$' .
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.
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.
You could use datetime.replace
to alter the second's attribute as shown:
df.index = df.index.map(lambda x: x.replace(second=0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With