Convert Integer to timedelta in Python (Example)

 

In this Python article you’ll learn how to transform an integer to timedelta.

Table of contents:

Let’s do this…

 

Creating Example Data

We first need to import the pandas package to Python:

import pandas as pd                                           # Import pandas

Next, I also have to create some example data:

data = pd.DataFrame({'x1':[5, 1, 8, 7, 5, 5],                 # Create pandas DataFrame
                     'x2':range(10, 16),
                     'x3':['a', 'b', 'c', 'c', 'a', 'c']})
print(data)                                                   # Print pandas DataFrame
#    x1  x2 x3
# 0   5  10  a
# 1   1  11  b
# 2   8  12  c
# 3   7  13  c
# 4   5  14  a
# 5   5  15  c

The previous output of the Python console shows the structure of our example data – We have constructed a pandas DataFrame with three columns.

In this specific tutorial, we want to change the data type of the column x1 to timedelta.

Let’s first check the current data type of this column:

print(data['x1'].dtype)                                       # Print data type of column
# int64

As you can see, the column x1 has the integer data type.

 

Example: Transforming pandas DataFrame Column from Integer to timedelta Using to_timedelta() Function

The following Python code explains how to switch the data type of a column from integer to timedelta.

Consider the following Python code and its output:

data_new = data.copy()                                        # Create copy of DataFrame
data_new['x1'] = pd.to_timedelta(data_new['x1'], unit = 'D')  # Transform integer to timedelta
print(data_new)                                               # Print updated DataFrame
#       x1  x2 x3
# 0 5 days  10  a
# 1 1 days  11  b
# 2 8 days  12  c
# 3 7 days  13  c
# 4 5 days  14  a
# 5 5 days  15  c

In the previous output, you can already see that the first variable x1 has been changed. The numbers are now shown with the day unit.

Let’s test the data type of our updated DataFrame column:

print(data_new['x1'].dtype)                                   # Print data type of updated column
# timedelta64[ns]

It’s a timedelta column, great!

 

Video, Further Resources & Summary

If you need further info on the Python programming syntax of this post, I recommend having a look at the following video on my YouTube channel. In the video, I demonstrate the Python code of this article:

 

The YouTube video will be added soon.

 

In addition, you might read the related articles on this website. A selection of posts that are related to the conversion of an integer to the timedelta data type can be found below.

 

You have learned in this tutorial how to convert an integer to the timedelta data type in Python. Don’t hesitate to let me know in the comments section below, if you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top