how to extract days as integers from a timedelta64[ns] object in python
This should convert your timedelta64[ns]
type to float64
representing days:
data['difference'].astype('timedelta64[D]')
You can use dt.days
to extract just days from your series,
df.difference
Out[117]:
0 -1 days +00:00:05
1 NaT
2 -1 days +00:00:05
3 1 days 00:00:00
dtype: timedelta64[ns]
df.difference.dt.days
Out[118]:
0 -1
1 NaN
2 -1
3 1
dtype: float64
All other component extracts,
dr
Out[93]:
0 -1 days +00:00:05
1 NaT
2 1 days 02:04:05
3 1 days 00:00:00
dtype: timedelta64[ns]
dr.dt.components
Out[95]:
days hours minutes seconds milliseconds microseconds nanoseconds
0 -1 0 0 5 0 0 0
1 NaN NaN NaN NaN NaN NaN NaN
2 1 2 4 5 0 0 0
3 1 0 0 0 0 0 0
According to pandas documentation, you can extract days using astype
method of timedelta64
object and the result type is float64
.
td.astype('timedelta64[D]')