Home > Net >  Timedelta Conversion is incomplete
Timedelta Conversion is incomplete

Time:05-09

I've been trying to work around the data type in my data frame. I have a column that measures duration in this format "HH:MM:SS". I have converted the data to timedelta type, and values above 24hrs e.g, "335:44:07" are not converting to days as I expect, rather is parsed as NaT. Please how do I make "335:44:07" to display as "13 days 09:44:07"?

CodePudding user response:

conversion should work from scratch using pandas.to_timedelta:

s = pd.Series(['335:44:07'])

pd.to_timedelta(s)

from a DataFrame:

df['col'] = pd.to_timedelta(df['col'])

output:

0   13 days 23:44:07
dtype: timedelta64[ns]

CodePudding user response:

Use to_timedelta:

print (pd.to_timedelta(["335:44:07"]))
TimedeltaIndex(['13 days 23:44:07'], dtype='timedelta64[ns]', freq=None)

For column:

df = pd.DataFrame({'col':['335:44:07']})
df['col'] = pd.to_timedelta(df["col"])
print (df)
               col
0 13 days 23:44:07
  • Related