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 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:
Dates | Status |
20210305 | Opened |
20210316 | Opened |
20210328 | Closed |
Step 2: Create the DataFrame
Next, create the DataFrame to capture the above dataset in Python:
import pandas as pd values = {'dates': [20210305,20210316,20210328], '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:
dates status
0 20210305 Opened
1 20210316 Opened
2 20210328 Closed
dates int64
status object
dtype: object
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': [20210305,20210316,20210328], '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:
dates status
0 2021-03-05 Opened
1 2021-03-16 Opened
2 2021-03-28 Closed
dates datetime64[ns]
status object
dtype: object
Converting Additional Formats
Let’s suppose that the dates are now formatted as yymmdd:
Dates | Status |
210305 | Opened |
210316 | Opened |
210328 | Closed |
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': [210305,210316,210328], '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:
dates status
0 2021-03-05 Opened
1 2021-03-16 Opened
2 2021-03-28 Closed
dates datetime64[ns]
status object
dtype: object
Now let’s suppose that your integers contain both the dates and times:
Dates | Status |
20210305093000 | Opened |
20210316093000 | Opened |
20210328200000 | Closed |
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': [20210305093000,20210316093000,20210328200000], '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:
dates status
0 2021-03-05 09:30:00 Opened
1 2021-03-16 09:30:00 Opened
2 2021-03-28 20:00:00 Closed
dates datetime64[ns]
status object
dtype: object
You may also want to check the following guide for the steps to convert strings to datetime in Pandas DataFrame.