How to remove microseconds from timedelta

Viewed 3197

I have microseconds that I want to essentially truncate from a pandas column. I tried something like analyze_me['how_long_it_took_to_order'] = analyze_me['how_long_it_took_to_order'].apply(lambda x: x.replace(microsecond=0) but to this error came up replace() takes no keyword arguments.

For example: I want 00:19:58.582052 to become 00:19:58 or 00:19:58.58

enter image description here

3 Answers

I needed this for a simple script where I wasn't using Pandas, and came up with a simple hack which should work everywhere.

age = age - timedelta(microseconds=age.microseconds)

where age is my timedelta object.

You can't directly modify the microseconds member of a timedelta object because it's immutable, but of course, you can replace it with another immutable object.

Related