Reputation: 131
I'm working in python with a pandas df and trying to convert a column that contains nanoseconds to a time with days, hours, minutes and seconds, but I'm not succeeding.
My original df looks like this:
ID TIME_NANOSECONDS
1 47905245000000000
2 45018244000000000
3 40182582000000000
The result should look like this:
ID TIME_NANOSECONDS TIME
1 47905245000000000 554 days 11:00:45.000000000
2 45018244000000000 521 days 01:04:04.000000000
3 40182582000000000 465 days 01:49:42.000000000
I've found some answers that advised to use timedelta, but following code, returns a date, which is not what I want.
temp_fc_col['TIME_TO_REPAIR_D'] = datetime.timedelta(temp_fc_col['TIME_TO_REPAIR'], unit='ns')
alternatively,
temp_fc_col['TIME_TO_REPAIR_D'] = (timedelta(microseconds=round(temp_fc_col['TIME_TO_REPAIR'], -3)
Returns an error: unsupported type for timedelta microseconds component: Series. Probably, because this staement can only process one value at a time.
Upvotes: 3
Views: 4363
Reputation: 863116
Use to_timedelta
working well with Series
, also unit='ns'
should be omit:
temp_fc_col['TIME_TO_REPAIR_D'] = pd.to_timedelta(temp_fc_col['TIME_NANOSECONDS'])
print (temp_fc_col)
ID TIME_NANOSECONDS TIME_TO_REPAIR_D
0 1 47905245000000000 554 days 11:00:45
1 2 45018244000000000 521 days 01:04:04
2 3 40182582000000000 465 days 01:49:42
Upvotes: 3