Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 793
Blue Moon Avatar asked Nov 09 '15 08:11

Blue Moon


3 Answers

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

data['difference'].astype('timedelta64[D]')
like image 74
Nhor Avatar answered Sep 24 '22 18:09

Nhor


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
like image 33
WoodChopper Avatar answered Sep 25 '22 18:09

WoodChopper


According to pandas documentation, you can extract days using astype method of timedelta64 object and the result is of type float64.

data['difference'].astype('timedelta64[D]')
like image 37
Kenly Avatar answered Sep 25 '22 18:09

Kenly