Pandas Convert DataFrame Column Type from Integer to datetime type datetime64[ns]
format – You can convert the Pandas DataFrame column type from integer to datetime format by using pandas.to_datetime() and DataFrame.astype() method. astype() method is used to cast from one type to another
In these pandas DataFrame article, I will explain how to convert integer holding date & time to datetime format using the above-mentioned methods and also using DataFrame.apply()
with lambda
function.
1. Quick Examples of Convert Integer to Datetime Format
If you are in a hurry, below are some quick examples of how to convert integer column type to datetime in Pandas DataFrame.
# Below are some quick examples
# 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')
Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate the results. Our DataFrame contains column names Courses
, Fee
and InsertedDate
.
# 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
.
2. 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.
3. 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.
4. Use DataFrame.astype() Method 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.
5. 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
6. 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, you have learned how to convert integer to datetime format by using pandas.to_datetime()
, DataFrame.astype()
, and DataFrame.apply()
with lambda
function with examples.
Happy Learning !!
Related Articles
- Select Pandas DataFrame Rows Between Two Dates
- How to Format Pandas Datetime?
- Pandas DatetimeIndex Usage Explained
- pandas Convert Datetime to Seconds
- Sort Pandas DataFrame by Date (Datetime)
- Pandas Extract Year from Datetime
- 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