How to convert a timedelta object to int in Pandas and Python?

Converting timedelta to days integers in Python

Use the dt.days property of the pandas library timedelta object to convert your timedeltas to days, hours, or minutes – all int64 objects. Here is a simple code snippet to use:

my-df[my_td_column] = my-df[my_td_column].dt.days

#for hours
my-df[my_td_column] = my-df[my_td_column].dt.days*24

# for minutes:
my-df[my_td_column] = my-df[my_td_column].dt.days*24*60

Create example data

We will start by creating some data that you can definitely use in order to follow along with this tutorial.

import pandas as pd

install_date = pd.Series(pd.date_range(start='1/1/25', periods = 5, freq='B'))
first_failure_date = pd.to_datetime(pd.Series( ['2026-02-24', '2025-03-15', '2025-10-11', '2026-08-12', '2026-10-23']))

# Create a dataframe
performance = pd.DataFrame(dict(install_date = install_date, first_failure_date = first_failure_date))

#Read the dataframe first rows
performance.head()

Let’s look into our data

install_datefirst_failure_date
02025-01-012026-02-24
12025-01-022025-03-15
22025-01-032025-10-11
32025-01-062026-08-12
42025-01-072026-10-23

Let’s look at the data types in our DataFrame:

performance.dtypes

This will render:

install_date          datetime64[ns]
first_failure_date    datetime64[ns]
dtype: object

Calculate the timedelta column

Using a vectorized operation, we will calculate a “time for first failure” column, by subtracting the two timestamp / datetime columns.

 performance ['time_to_first_failure'] = performance ['first_failure_date'] - performance['install_date']

performance.head()

Looking our DataFrame we see a new column:

install_datefirst_failure_datetime_to_first_failure
02025-01-012026-02-24419 days
12025-01-022025-03-1572 days
22025-01-032025-10-11281 days
32025-01-062026-08-12583 days
42025-01-072026-10-23654 days

The data types are:

install_date              datetime64[ns]
first_failure_date        datetime64[ns]
time_to_first_failure    timedelta64[ns]
dtype: object

Convert timedelta objects to integers

We would like to convert the last column in our pandas DataFrame from time delta to int object types. If we try to use astype() to convert the timedelta to int32, Python will render an exception:

# will render a type error
performance['time_first_failure'].astype('int32')

The following error will be displayed:

TypeError: cannot astype a datetimelike from [timedelta64[ns]] to [int32]

Cast a timedelta object to a datetime

Converting a timedelta to datetime makes little sense, and will render the following error message:

TypeError: dtype timedelta64[ns] cannot be converted to datetime64[ns]

Most probably you might want to add or subtract a timedelta from a date.

performance['install_date'] = performance['first_failure_date'] - performance['time_to_first_failure']

Convert timedelta to days

We use the dt accesor of your timedelta series object to return the number of days (will be returned as an int64 data type):

performance['time_to_first_failure'] = performance['time_to_first_failure'].dt.days

Convert timedelta to months (integer values)

(performance['time_to_first_failure'].dt.days/30).astype('int64')

Cast timedelta to float numbers

Let’s use the conversion to months as an example on how to turn timedelta to floats:

round(performance['time_to_first_failure'].dt.days/30, 2)

This will return the following Series of floats / decimals:

0    13.97
1     2.40
2     9.37
3    19.43
4    21.80
Name: time_to_first_failure, dtype: float64

Follow up learning

How to make a Python datetime object from a pandas timestamp?