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

后端 未结 3 2001
南方客
南方客 2021-02-04 19:19

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 d         


        
相关标签:
3条回答
  • 2021-02-04 19:29

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

    td.astype('timedelta64[D]')
    
    0 讨论(0)
  • 2021-02-04 19:34

    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
    
    0 讨论(0)
  • 2021-02-04 19:46

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

    data['difference'].astype('timedelta64[D]')
    
    0 讨论(0)
提交回复
热议问题