How to Convert Integers to Datetime in Pandas DataFrame

Here is the syntax that you may use to convert integers to datetime in Pandas DataFrame:

df['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)

Note that the integers data must match the format specified. Later, you’ll see several scenarios for different formats.

Steps to Convert Integers to Datetime in Pandas DataFrame

Step 1: Gather the data to be converted to datetime

To start, gather the data that you’d like to convert to datetime.

For example, the following dataset contains 3 different dates (with a format of yyyymmdd), when a store might be opened or closed:

DatesStatus
20190902Opened
20190913Opened
20190921Closed

Step 2: Create the DataFrame

Next, create the DataFrame to capture the above dataset in Python:

import pandas as pd

values = {'dates':  [20190902,20190913,20190921],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame(values, columns = ['dates','status'])

print (df)
print (df.dtypes)

As you may see, the data type is integer for the values under the ‘dates’ column:

How to Convert Integers to Datetime in Pandas DataFrame

Step 3: Convert the integers to datetime in Pandas DataFrame

Now you may use the template below in order to convert the integers to datetime in Pandas DataFrame:

df['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)

Recall that for our example, the date format is yyyymmdd.

In that case, the date format can be represented as follows:

format='%Y%m%d'

As indicated previously, the integer data (yyyymmdd) must match the format specified (%Y%m%d). You may refer to the following source for the different formats that you may apply.

For our example, the complete code to convert the integers to datetime would be:

import pandas as pd

values = {'dates':  [20190902,20190913,20190921],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame(values, columns = ['dates','status'])

df['dates'] = pd.to_datetime(df['dates'], format='%Y%m%d')

print (df)
print (df.dtypes)

Run the code in Python, and you’ll see that the data type for the ‘dates’ is now datetime:

How to Convert Integers to Datetime in Pandas DataFrame

Converting Additional Formats

Let’s suppose that the dates are now formatted as yymmdd:

DatesStatus
190902Opened
190913Opened
190921Closed

In that case, the date format would now contain ‘y‘ in lower case:

format='%y%m%d'

So the complete Python code would look as follows:

import pandas as pd

values = {'dates':  [190902,190913,190921],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame(values, columns = ['dates','status'])

df['dates'] = pd.to_datetime(df['dates'], format='%y%m%d')

print (df)
print (df.dtypes)

As before, the integers would get converted to datetime:

Convert Integers to Datetime in Pandas DataFrame

Now let’s suppose that your integers contain both the dates and times:

DatesStatus
20190902093000Opened
20190913093000Opened
20190921200000Closed

In that case, the format that you should specify is:

format='%Y%m%d%H%M%S'

So the full Python code would be:

import pandas as pd

values = {'dates':  [20190902093000,20190913093000,20190921200000],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame(values, columns = ['dates','status'])

df['dates'] = pd.to_datetime(df['dates'], format='%Y%m%d%H%M%S')

print (df)
print (df.dtypes)

You’ll now get the datetime format:

datetime example

You may also want to check the following guide for the steps to convert strings to datetime in Pandas DataFrame.