Is there a way to fix or bypass weird time formats in a specific column in a dataframe?

Viewed 37

I am working with a SLURM dataset in Pandas that has time formats like so in the 'Elapsed' column:

00:00:00
00:26:51

However, sometimes there are sections that are greater than 24 hours, and it displays it like so:

1-00:02:00
3-01:25:02

I want to find the mean of the entire column but it mishandles the to_timedelta conversion on the entries with entries above 24 hours like shown above. One example is this:

Before to_timedelta: 3-01:25:02
after to_timedelta: -13 days +10:34:58

I cannot simply convert the column into a new format because when entry is not greater than 24 hours, preceding zeros do not exist, ex: 0-20:00:00 This method would be easiest I believe if there is a way however.

Is there a way to fix this conversion or any other ideas on approaching this?

1 Answers

One way to go around is replacing - with days:

pd.to_timedelta(df['time'].str.replace('-','days'))

Output (for 4 lines above):

0   0 days 00:00:00
1   0 days 00:26:51
2   1 days 00:02:00
3   3 days 01:25:02
Name: time, dtype: timedelta64[ns]
Related