Table of Contents
Pandas is a popular library in Python used for data manipulation and analysis. One common task in data analysis is converting time durations, also known as Timedeltas, to integers. This can be easily achieved in Pandas using the “total_seconds()” function, which converts the Timedelta object to a floating-point value representing the total number of seconds. This value can then be converted to an integer using the “int()” function.
For example, if we have a Timedelta object representing 2 hours and 30 minutes, we can convert it to an integer of 9000 seconds by using the following code:
timedelta = pd.Timedelta(‘2 hours 30 minutes’)
integer = int(timedelta.total_seconds())
print(integer) # output: 9000
Some other examples of converting Timedeltas to integers in Pandas include calculating the total number of days, hours, or minutes in a Timedelta object, or converting a Timedelta column in a Pandas DataFrame to an integer column for easier manipulation and analysis.
Convert Timedelta to Int in Pandas (With Examples)
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 DataFrameprint(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.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-timedelta-to-an-integer-in-pandas-and-what-are-some-examples-of-doing-so/
stats writer. "How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-timedelta-to-an-integer-in-pandas-and-what-are-some-examples-of-doing-so/.
stats writer. "How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-timedelta-to-an-integer-in-pandas-and-what-are-some-examples-of-doing-so/.
stats writer (2024) 'How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-timedelta-to-an-integer-in-pandas-and-what-are-some-examples-of-doing-so/.
[1] stats writer, "How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a Timedelta to an integer in Pandas, and what are some examples of doing so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
