How to Convert Integer to Datetime in Pandas DataFrame?

10 Jul, 2020
Let’s discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame, we can use the following syntax:
df[‘DataFrame Column’] = pd.to_datetime(df[‘DataFrame Column’], format=specify your format)
Note: The integers data must match the format specified.
Example #1:
Python
# importing pandas packageimport pandas as pd  # creating a dataframevalues = {'Dates':  [20190902, 20190913, 20190921],          'Attendance': ['Attended', 'Not Attended', 'Attended']          }  df = pd.DataFrame(values, columns=['Dates', 'Attendance'])  # displayprint(df)print(df.dtypes)
Output:
As we can see,  the data type for the ‘Dates’ column is Integer. Now to convert it into Datetime we use the previously mentioned syntax. Since in this example the date format is yyyymmdd, the date format can be represented as follows:
format= '%Y%m%d'

For our example, the complete code to convert the integers to DateTime would be:
Python
# importing pandas packageimport pandas as pd  # creating the dataframevalues = {'Dates':  [20190902, 20190913, 20190921],          'Attendance': ['Attended', 'Not Attended', 'Attended']          }  df = pd.DataFrame(values, columns=['Dates', 'Attendance'])  # converting the integers to datetime formatdf['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d')  # displayprint(df)print(df.dtypes)
Output:
Example #2: Now, suppose the Dataframe has a date in format yymmdd. In this case, the date format would now contain ‘y’ in lowercase:
format='%y%m%d'

So the complete Python code would look as follows:
Python
# importing pandas packageimport pandas as pd  # creating dataframevalues = {'Dates':  [190902, 190913, 190921],          'Attendance': ['Attended', 'Not Attended', 'Attended']          }  df = pd.DataFrame(values, columns=['Dates', 'Attendance'])  # changing the integer dates to datetime formatdf['Dates'] = pd.to_datetime(df['Dates'], format='%y%m%d')  # displayprint(df)print(df.dtypes)
Output:
Example #3: Now, let’s suppose that your integers contain both date and time. In that case, the format that you should specify is:
format='%Y%m%d%H%M%S'

So the full Python code would be:
Python
# importing pandas packageimport pandas as pd  # creating dataframevalues = {'Dates': [20190902093000, 20190913093000, 20190921200000],          'Attendance': ['Attended', 'Not Attended', 'Attended']          }  df = pd.DataFrame(values, columns=['Dates', 'Attendance'])  # changing integer values to datetime formatdf['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S')  # displayprint(df)print(df.dtypes)
Output:
Example #4: Consider this DataFrame with microseconds in our DateTime values. In this case, the format should be specified as:
format='%Y%m%d%H%M%S%F'

So the full Python code will be:
Python
# importing pandas packageimport pandas as pd  # creating dataframevalues = {'Dates':  [20190902093000912, 20190913093000444,                      20190921200000009],          'Attendance': ['Attended', 'Not Attended', 'Attended']          }  df = pd.DataFrame(values, columns=['Dates', 'Attendance'])  # changing the integer dates to datetime formatdf['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S%F')  # displayprint(df)print(df.dtypes)
Output:
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe