206

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

1
  • 24
    Have you tried to use timedelta.days? Commented Sep 3, 2014 at 13:53

6 Answers 6

280

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I like this comment for the simplicity and not requiring import of another library.
Make sue you use this on a Series. It does not work on a DataFrame.
79

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)

3 Comments

Thanks! Also after 15 more minutes of searching I found this. stackoverflow.com/questions/18215317/…
what is the / for between td and np?
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
45

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

1 Comment

No .hours or .minutes though, so you will need to do some math if you want values in those units.
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')

2 Comments

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???
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
5

A great way to do this is

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

1 Comment

this answer is similar to a previous one, consider adding a comment instead of answering again
3

The simplest way to do this is by

df["DateColumn"] = (df["DateColumn"]).dt.days

2 Comments

this solution is not different from already proposed ones.
Yes you are right, I am only pointing out the easiest amongst the solutions I know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.