how to extract days as integers from a timedelta64[ns] object in python

Blue Moon picture Blue Moon · Nov 9, 2015 · Viewed 17.5k times · Source

I have a pandas dataframe with a column like:

In [96]: data['difference']
Out[96]: 
0                 NaT
1     1 days 21:34:30
2     0 days 16:57:36
3     0 days 00:16:51
4     0 days 15:52:38
5     0 days 14:19:34
6     0 days 02:54:46
7     1 days 04:21:28
8     0 days 01:58:55
9     0 days 10:30:35
10    0 days 07:53:04
....
Name: difference, dtype: timedelta64[ns]

I want to create next to it a column with integers corresponding to the days values in this column.

Answer

Nhor picture Nhor · Nov 9, 2015

This should convert your timedelta64[ns] type to float64 representing days:

data['difference'].astype('timedelta64[D]')