pandas – Python: Convert timedelta to int in a dataframe
pandas – Python: Convert timedelta to int in a dataframe
The Series class has a pandas.Series.dt
accessor object with several
useful 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)
pandas – Python: Convert timedelta to int in a dataframe
Timedelta objects have read-only instance attributes .days
, .seconds
, and .microseconds
.