Python: Convert timedelta to int in a dataframe Python: Convert timedelta to int in a dataframe python python

Python: Convert timedelta to int in a dataframe


The Series class has a pandas.Series.dt accessor object with severaluseful datetime attributes, including dt.days. Access this attribute via:

timedelta_series.dt.days

You can also get the seconds and microseconds attributes in the same way.


You could do this, where td is your series of timedeltas. The division converts the nanosecond deltas into day deltas, and the conversion to int drops to whole days.

import numpy as np(td / np.timedelta64(1, 'D')).astype(int)


Timedelta objects have read-only instance attributes .days, .seconds, and .microseconds.