user7468395
user7468395

Reputation: 1349

Why does pd.to_timedelta fail when supplying an int series?

I would like to convert the following int series to a timedelta. I would expect that the result would be 16h30s00, but it is something else.

What do I have to do to convert my dataframe to 16h30s00?

a = pd.DataFrame({'b':[163000,163000]})
pd.to_timedelta(a.b,'S')

Out[87]: 
0   1 days 21:16:40
1   1 days 21:16:40
Name: b, dtype: timedelta64[ns]

Upvotes: 1

Views: 48

Answers (1)

Shehan Ishanka
Shehan Ishanka

Reputation: 593

Try this.

a = pd.DataFrame({'b':[163000,163000]})
k=pd.to_datetime(a.b,format='%H%S%f')
k=k.dt.strftime('%H:%M:%S.%f')
print(pd.to_timedelta(k,unit='S'))

OUTPUT:

0   16:00:30
1   16:00:30
Name: b, dtype: timedelta64[ns]

Upvotes: 1

Related Questions