In Pandas, you can convert an integer column representing dates or times into a DateTime type using the pd.to_datetime()
function. To convert integer to datetime type datetime64[ns] format by using pandas.to_datetime() and DataFrame.astype() methods. astype()
method is used to cast from one type to another.
In these pandas DataFrame article, I will explain converting integer holding date & time to datetime format using the above-mentioned methods and also using DataFrame.apply()
with lambda
function.
Quick Examples of Convert Integer to Datetime Format
Below are quick examples of converting integer column type to datetime.
# Quick examples of convert integer to datetime format
# Example 1: Convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d')
# Example 2: Use pandas.to_datetime() and DataFrame.apply() with lambda function
df['InsertedDate'] = df['InsertedDate'].apply(lambda x: pd.to_datetime(str(x),format='%Y%m%d'))
# Example 3: Use series.astype() method to convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'].astype(str), format='%Y%m%d')
# Example 4: Use pandas.to_datetime() to convert integers to "yymmdd" format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%y%m%d')
# Example 5: Changing integer values to dates and time format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d%H%M%S')
To run some examples of converting an integer to a Datetime type in pandas DataFrame, let’s create Pandas DataFrame.
# Create DataFrame
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop"],
'Fee' :[22000.0,25000.0,23000.0],
'InsertedDate':[20201124,20210210,20211215]
})
df = pd.DataFrame(technologies)
print("Create Dataframe:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)
Yields below output.
As you see above, you can get the data types of all columns using df.dtypes
. You can also get the same using df.infer_objects().dtypes
.
Convert Integer to Datetime Format
In the below example, note that the data type for the ‘InsertedDate’
column is Integer. To convert it into Datetime, I use pandas.to_datetime(). This method takes a parm format to specify the format of the date you wanted to convert from. Here, the InsertedDate
column has the date in the format yyyymmdd
,
# Convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d')
print("After converting integer to datetime:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)
Yields below output.
Use Series.apply() with Lambda Function
You can also use pandas.to_datetime()
and DataFrame.apply()
with lambda function to convert integer to datetime.
# Use pandas.to_datetime() and DataFrame.apply() with lambda function
df['InsertedDate'] = df['InsertedDate'].apply(lambda x: pd.to_datetime(str(x),format='%Y%m%d'))
print("After converting integer to datetime:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)
Yields the same output as above.
Use DataFrame.astype() to Convert Integer to Datetime Format
DataFrame.astype()
method is also used to convert integer to datetime format. The dtype of this datetime column would be datetime64[ns]
.
# Use series.astype() method to convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'].astype(str), format='%Y%m%d')
print("After converting integer to datetime:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)
Yields the same output as above.
Use pandas.to_datetime() to Convert Integer to Date & Time Format
Let’s suppose that your integers contain both the date and time. In that case, the format should be specified is '%Y%m%d%H%M%S'
.
# Use pandas.to_datetime() to Convert Integer to Date & Time Format
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop"],
'Fee' :[22000.0,25000.0,23000.0],
'InsertedDate':[20201124063015,20210210084021,20211215032511]
})
df = pd.DataFrame(technologies)
# changing integer values to dates and time format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d%H%M%S')
print("After converting integer to datetime:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)
Yields below output.
# Output:
# After converting integer to datetime:
Courses Fee InsertedDate
0 Spark 22000.0 2020-11-24 06:30:15
1 PySpark 25000.0 2021-02-10 08:40:21
2 Hadoop 23000.0 2021-12-15 03:25:11
------------------------------------------
Type of the columns:
Courses object
Fee float64
InsertedDate datetime64[ns]
dtype: object
Complete Example For Convert Integers to Datetime Format
# Example For Convert Integers to Datetime Format
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop"],
'Fee' :[22000.0,25000.0,23000.0],
'InsertedDate':[20201124,20210210,20211215]
})
df = pd.DataFrame(technologies)
print(df)
print(df.dtypes)
# Convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d')
print(df)
print(df.dtypes)
# Use pandas.to_datetime() and DataFrame.apply() with lambda function
df['InsertedDate'] = df['InsertedDate'].apply(lambda x: pd.to_datetime(str(x),format='%Y%m%d'))
print(df)
print(df.dtypes)
# Use series.astype() method
# To convert integers to datetime format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'].astype(str), format='%Y%m%d')
print(df)
print(df.dtypes)
# Use pandas.to_datetime()
# To convert integers to "yymmdd" format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%y%m%d')
print(df)
print(df.dtypes)
# Changing integer values to dates and time format
df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d%H%M%S')
print(df)
print(df.dtypes)
Conclusion
In this article, I have explained converting integers to DateTime format by using pandas.to_datetime()
, astype()
, and apply()
with lambda
method with some examples.
Happy Learning !!
Related Articles
- Pandas Get Day, Month and Year from DateTime
- Pandas Extract Month and Year from Datetime
- Pandas Convert Datetime to Date Column
- Pandas Convert Date (DateTime) to String Format
- Pandas Convert Multiple Columns To DateTime Type
- Pandas Convert Column To DateTime
- Pandas DatetimeIndex Examples
- Pandas Convert JSON to DataFrame
- Pandas Convert Column to Int in DataFrame
- Convert PySpark DataFrame to Pandas
- convert pandas to pyspark dataframe
- pandas convert column to numpy array