How to Convert Timedelta to Int in Pandas? (With Examples)

In Pandas, you can convert a timedelta object to an integer number of units (such as days, hours, minutes, etc.) by using the total_seconds() method. The result is returned as a float. To convert the float result to an integer, you can use the int() function. You can also use the astype() method to convert the timedelta object to an integer. Examples of how to convert timedelta to int in Pandas are shown below.


You can use the following methods to convert a timedelta column to an integer column in a pandas DataFrame:

Method 1: Convert Timedelta to Integer (Days)

df['days'] = df['timedelta_column'].dt.days

Method 2: Convert Timedelta to Integer (Hours)

df['hours'] = df['timedelta_column'] / pd.Timedelta(hours=1)

Method 3: Convert Timedelta to Integer (Minutes)

df['minutes'] = df['timedelta_column'] / pd.Timedelta(minutes=1)

The following example shows how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'promotion': ['A', 'B', 'C', 'D'],
                   'start': ['2021-10-04 13:29:00', '2021-10-07 12:30:00',
                             '2021-10-15 04:20:00', '2021-10-18 15:45:03'],
                   'end':   ['2021-10-08 11:29:06', '2021-10-15 10:30:07',
                             '2021-10-29 05:50:15', '2021-10-22 15:40:03']})

#convert start date and end date columns to datetime
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])

#create new column that contains timedelta between start and end
df['duration'] = df['end'] - df['start']

#view DataFrame
print(df)

  promotion               start                 end         duration
0         A 2021-10-04 13:29:00 2021-10-08 11:29:06  3 days 22:00:06
1         B 2021-10-07 12:30:00 2021-10-15 10:30:07  7 days 22:00:07
2         C 2021-10-15 04:20:00 2021-10-29 05:50:15 14 days 01:30:15
3         D 2021-10-18 15:45:03 2021-10-22 15:40:03  3 days 23:55:00

Example 1: Convert Timedelta to Integer (Days)

The following code shows how to create a new column called days that converts the timedelta in the duration column into an integer value that represents the number of days in the timedelta column.

#create new column that converts timedelta into integer number of days
df['days'] = df['duration'].dt.days

#view updated DataFrame
print(df)

  promotion               start                 end         duration  days
0         A 2021-10-04 13:29:00 2021-10-08 11:29:06  3 days 22:00:06     3
1         B 2021-10-07 12:30:00 2021-10-15 10:30:07  7 days 22:00:07     7
2         C 2021-10-15 04:20:00 2021-10-29 05:50:15 14 days 01:30:15    14
3         D 2021-10-18 15:45:03 2021-10-22 15:40:03  3 days 23:55:00     3

We can use dtype to check the data type of this new column:

#check data type
df.days.dtype

dtype('int64')

The new column is an integer.

Example 2: Convert Timedelta to Integer (Hours)

The following code shows how to create a new column called hours that converts the timedelta in the duration column into a numeric value that represents the total number of hours in the timedelta column.

#create new column that converts timedelta into total number of hours
df['hours'] = df['duration'] / pd.Timedelta(hours=1)

#view updated DataFrame
print(df)

  promotion               start                 end         duration      hours
0         A 2021-10-04 13:29:00 2021-10-08 11:29:06  3 days 22:00:06   94.001667  
1         B 2021-10-07 12:30:00 2021-10-15 10:30:07  7 days 22:00:07  190.001944
2         C 2021-10-15 04:20:00 2021-10-29 05:50:15 14 days 01:30:15  337.504167
3         D 2021-10-18 15:45:03 2021-10-22 15:40:03  3 days 23:55:00   95.916667

We can use dtype to check the data type of this new column:

#check data type
df.hours.dtype

dtype('float64')

The new column is a float.

Example 3: Convert Timedelta to Integer (Minutes)

The following code shows how to create a new column called minutes that converts the timedelta in the duration column into a numeric value that represents the total number of minutes in the timedelta column.

#create new column that converts timedelta into total number of minutes
df['minutes'] = df['duration'] / pd.Timedelta(minutes=1)

#view updated DataFrame
print(df)

  promotion               start                 end         duration        minutes
0         A 2021-10-04 13:29:00 2021-10-08 11:29:06  3 days 22:00:06    5640.100000  
1         B 2021-10-07 12:30:00 2021-10-15 10:30:07  7 days 22:00:07   11400.116667
2         C 2021-10-15 04:20:00 2021-10-29 05:50:15 14 days 01:30:15   20250.250000
3         D 2021-10-18 15:45:03 2021-10-22 15:40:03  3 days 23:55:00    5755.000000

We can use dtype to check the data type of this new column:

#check data type
df.minutes.dtype

dtype('float64')

The new column is a float.

x