How to convert int to datetime Pandas Dataframe

In this post, we will learn How to convert an int to datetime in Pandas Dataframe or convert int64 to datetime in Pandas. The Pandas is a data analytical library that store data in tabular form, and the table in Pandas is called a dataframe that contains rows and column. It is great to help explore, clean and process data. It supports the integration of different file formats “csv, excel, SQL, JSON, parquet,”.To use a Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.

1.How to convert int to datetime Pandas Dataframe


In this example, The dataframe column ‘Admission_date’ contains the value of date as an integer. To convert integer date value to date the Pandas dataframe pd.to_datetime() function is used that takes the first parameter is the column name, the Second parameter is the format of date in which, need to convert. The date format here is ‘%m%d%y’ which is equal to Python pandas integer YYYYMMDD to datetime.

import pandas as pd
import numpy as np
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy'],
    'Admission_date':[2022615.0,2022617.0,2022616.0,2022618.0]
    }


df = pd.DataFrame(data)

print('Existing datatype:\n',df.dtypes)


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


print('\n',df)

print('datatype after conversion:',df.dtypes)

Output

Existing datatype:
 Name               object
Marks               int64
Subject            object
Admission_date    float64
dtype: object

     Name  Marks Subject Admission_date
0   Jack     97    Math     2022-06-15
1   Rack     97    Math     2022-06-17
2    Max    100    Math     2022-06-16
3  David    100     Phy     2022-06-18
datatype after conversion: Name                      object
Marks                      int64
Subject                   object
Admission_date    datetime64[ns]
dtype: object

2.How to convert int to datetime Pandas Dataframe


The given dataframe column ‘Admission_date’ contains the value of date as an integer. To convert integer date value to date the Pandas dataframe pd.to_datetime() function is used that takes the first parameter is the column name, the Second parameter is the format of date in which, need to convert. The date format here is ‘%y%m%d’ which is equal to yyyymmdd.

import pandas as pd
import numpy as np
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy'],
    'Admission_date':[220715,220515,220613,220616]


df = pd.DataFrame(data)

print('Existing datatype:\n',df .dtypes)


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


print('\n',df)

print('\n datatype after conversio:',df .dtypes)


Output

Existing datatype:
 Name              object
Marks              int64
Subject           object
Admission_date     int64
dtype: object

     Name  Marks Subject Admission_date
0   Jack     97    Math     2022-07-15
1   Rack     97    Math     2022-05-15
2    Max    100    Math     2022-06-13
3  David    100     Phy     2022-06-16


datatype after conversion:


 Name                      object
Marks                      int64
Subject                   object
Admission_date    datetime64[ns]
dtype: object

3.How to convert int to datetime Pandas Dataframe


The dataframe column ‘Admission_date’ contains the date and time value as an integer. So without losing the value time, we will have to pass the format value “%y%m%d%H%M%S” to the to_datetime() method of Pandas dataframe that is equal to yyyymmdd hh:mm:ss. Sometimes the integer date value contains time in microseconds, the format used for microseconds is “%y%m%d%H%M%S%F”.

import pandas as pd
import numpy as np
 
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy'],
    'Admission_date':[2207150634,2205151236,2206130169,2206160337]
}



df = pd.DataFrame(data)

print('Existing datatype:\n',df .dtypes)


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


print('\n',df)

print('\n datatype after conversio:',df .dtypes)


Output

Existing datatype:
 Name              object
Marks              int64
Subject           object
Admission_date     int64
dtype: object

     Name  Marks Subject      Admission_date
0   Jack     97    Math 2022-07-15 06:03:04
1   Rack     97    Math 2022-05-15 12:03:06
2    Max    100    Math 2022-06-13 01:06:09
3  David    100     Phy 2022-06-16 03:03:07

 datatype after conversio: Name                      object
Marks                      int64
Subject                   object
Admission_date    datetime64[ns]
dtype: object

4.How to convert int to datetime Pandas Dataframe


This Python program performs the same operation as we have done in the above example instead of a simple approach we have used lambda and applied the method of pandas dataframe to convert int to data time. It is a short and one-line code.

df = pd.DataFrame(data)

print('Existing datatype:\n',df .dtypes)


#df['Admission_date'] = pd.to_datetime(df['Admission_date'],format='%y%m%d%H%M%S')

df['Admission_date'] = df['Admission_date'].apply(lambda x: pd.to_datetime(x, format='%y%m%d%H%M%S'))


print(df)

print('\n datatype after conversio:\n',df .dtypes)


Output

Existing datatype:
 Name              object
Marks              int64
Subject           object
Admission_date     int64
dtype: object
    Name  Marks Subject      Admission_date
0   Jack     97    Math 2022-07-15 06:03:04
1   Rack     97    Math 2022-05-15 12:03:06
2    Max    100    Math 2022-06-13 01:06:09
3  David    100     Phy 2022-06-16 03:03:07

 datatype after conversio:
 Name                      object
Marks                      int64
Subject                   object
Admission_date    datetime64[ns]
dtype: object