Plotting pandas timedelta

Here are ways to convert timedeltas, docs are here

In [2]: pd.to_timedelta(np.arange(5),unit="d")+pd.to_timedelta(1,unit="s")
Out[2]: 
0   0 days, 00:00:01
1   1 days, 00:00:01
2   2 days, 00:00:01
3   3 days, 00:00:01
4   4 days, 00:00:01
dtype: timedelta64[ns]

Convert to seconds (is an exact conversion)

In [3]: (pd.to_timedelta(np.arange(5),unit="d")+pd.to_timedelta(1,unit="s")).astype('timedelta64[s]')
Out[3]: 
0         1
1     86401
2    172801
3    259201
4    345601
dtype: float64

Convert using astype will round to that unit

In [4]: (pd.to_timedelta(np.arange(5),unit="d")+pd.to_timedelta(1,unit="s")).astype('timedelta64[D]')
Out[4]: 
0    0
1    1
2    2
3    3
4    4
dtype: float64

Division will give an exact repr

In [5]: (pd.to_timedelta(np.arange(5),unit="d")+pd.to_timedelta(1,unit="s")) / np.timedelta64(1,'D')
Out[5]: 
0    0.000012
1    1.000012
2    2.000012
3    3.000012
4    4.000012
dtype: float64

Leave a Comment