197

I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use 'datetime.days' or do I need to do something more manual?

timedelta column

7 days, 23:29:00

day integer column

7

Asaf Hanish
  • 1,989
  • 2
  • 11
  • 6

6 Answers6

269

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.

Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
abeboparebop
  • 7,396
  • 6
  • 37
  • 46
78

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)
chrisb
  • 49,833
  • 8
  • 70
  • 70
  • 1
    Thanks! Also after 15 more minutes of searching I found this. http://stackoverflow.com/questions/18215317/extracting-days-from-a-numpy-timedelta64-value – Asaf Hanish Sep 03 '14 at 14:13
  • what is the `/` for between `td` and `np`? – Jia Gao Mar 20 '19 at 12:41
  • It's the timedelta64 division operator. Dividing td by a 1 day time delta is results in the (possibly fractional) number of days represented in td. Not required in this case but it's really useful if say you want to work out how many 15 minute intervals td represents – David Waterworth Jul 13 '19 at 05:29
43

Timedelta objects have read-only instance attributes .days, .seconds, and .microseconds.

Qiao Zhang
  • 569
  • 4
  • 9
  • 1
    No `.hours` or `.minutes` though, so you will need to do some math if you want values in those units. – Nick Jan 15 '21 at 18:34
22

If the question isn't just "how to access an integer form of the timedelta?" but "how to convert the timedelta column in the dataframe to an int?" the answer might be a little different. In addition to the .dt.days accessor you need either df.astype or pd.to_numeric

Either of these options should help:

df['tdColumn'] = pd.to_numeric(df['tdColumn'].dt.days, downcast='integer')

or

df['tdColumn'] = df['tdColumn'].dt.days.astype('int16')
CheapSquier
  • 427
  • 4
  • 4
  • Hi, I tried this, but I got ValueError: Cannot convert non-finite values (NA or inf) to integer because there are nans in the pandas series. Do you know who to sort this out??? – Pablito Dec 10 '19 at 02:28
  • The second option worked for me and the date values were of type `timedelta64[ns]`. If your dates are NaN, first convert them to datetime using the pandas `to_datetime` function, then use the second option above. For more details checkout [to_datetime](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html?highlight=to_datetime) – Onen simon Jul 29 '20 at 10:23
4

A great way to do this is

dif_in_days = dif.days (where dif is the difference between dates)

Funny Kup
  • 39
  • 3
3

The simplest way to do this is by

df["DateColumn"] = (df["DateColumn"]).dt.days
donDrey
  • 41
  • 4