Python: Convert timedelta to int in a dataframe

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

6 Answers

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.

2

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

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

1

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

I think it's much easier way to it with this (where dif is the difference between dates):

dif_In_Days = dif.days 
1

The simplest way to do this is by

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like