Let’s see different ways to convert multiple columns from string, integer, and object to DataTime (date & time) type using pandas.to_datetime(), DataFrame.apply()
& astype() functions. Usually, we get Data & time from the sources in different formats and in different data types, by using these functions you can convert them to a data time type datetime64[ns]
of pandas.
In this article, I will explain how to change multiple columns to DateTime type datetime64[ns] with examples.
1. Quick Examples of Convert Multiple Columns To DateTime
If you are in a hurry, below are some quick examples of how to convert multiple columns to DateTime in pandas DataFrame.
# Below are quick examples
# Use DataFrame.apply() to convert multiple columns to datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime)
# Convert pandas multiple columns to Datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime, errors='coerce')
# Convert multiple column to datetime using astype() method
df[['Inserted','Updated']] = df[['Inserted','Updated']].astype(datetime64[ns])
Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, Discount
, Inserted
and Updated
.
# Create DataFrame
import pandas as pd
from datetime import datetime, timedelta
from pandas import DataFrame
df = DataFrame.from_dict(
{'Courses':["Spark","Hadoop","pandas"],
'Fee' :[20000,25000,30000],
'Duration':['30days','40days','35days'],
'Discount':[1000,2500,1500],
'Inserted': ["10/02/2021, 10:39:24","09/12/2021, 08:09:24","01/22/2021, 10:29:14"],
'Updated': ["11/12/2021, 11:39:24","10/22/2021, 10:39:34","05/12/2021, 04:49:04"]},
orient='index',
columns=['A', 'B', 'C']).T
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount Inserted Updated
A Spark 20000 30days 1000 10/02/2021, 10:39:24 11/12/2021, 11:39:24
B Hadoop 25000 40days 2500 09/12/2021, 08:09:24 10/22/2021, 10:39:34
C pandas 30000 35days 1500 01/22/2021, 10:29:14 05/12/2021, 04:49:04
2. Use DataFrame.apply() to Convert String Multiple Columns to DateTime
You can use the DataFrame.apply()
and pd.to_datetime()
function to convert multiple columns to DataTime. apply() function applies function to each and every row and column of the DataFrame.
# Use DataFrame.apply() to convert multiple columns to datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount Inserted Updated
A Spark 20000 30days 1000 2021-10-02 10:39:24 2021-11-12 11:39:24
B Hadoop 25000 40days 2500 2021-09-12 08:09:24 2021-10-22 10:39:34
C pandas 30000 35days 1500 2021-01-22 10:29:14 2021-05-12 04:49:04
Now let’s convert Inserted
and Updated
columns to DateTime type.
# Convert pandas multiple columns to Datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime, errors='coerce')
print(df)
Yields same output as above.
3. Convert Multiple Column to DateTime Using astype() Method
Use series.astype()
method to convert the multiple columns to date & time type. First, select all the columns you wanted to convert and use astype() function with the type you wanted to convert as a param. astype() is also used to convert data types (String to int e.t.c) in pandas DataFrame
# Convert multiple column to datetime using astype() method
df[['Inserted','Updated']] = df[['Inserted','Updated']].astype(datetime64[ns])
print(df)
Yields same output as above.
4. Complete Example – Convert Multiple Columns To DateTime
import pandas as pd
from datetime import datetime, timedelta
from pandas import DataFrame
df = DataFrame.from_dict(
{'Courses':["Spark","Hadoop","pandas"],
'Fee' :[20000,25000,30000],
'Duration':['30days','40days','35days'],
'Discount':[1000,2500,1500],
'Inserted': ["10/02/2021, 10:39:24","09/12/2021, 08:09:24","01/22/2021, 10:29:14"],
'Updated': ["11/12/2021, 11:39:24","10/22/2021, 10:39:34","05/12/2021, 04:49:04"]},
orient='index',
columns=['A', 'B', 'C']).T
print(df)
# Use DataFrame.apply() to convert multiple columns to datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime)
print(df)
# Convert pandas multiple columns to Datetime
df[['Inserted','Updated']] = df[['Inserted','Updated']].apply(pd.to_datetime, errors='coerce')
print(df)
# Convert multiple column to datetime using astype() method
df[['Inserted','Updated']] = df[['Inserted','Updated']].astype(str)
print(df)
Conclusion
In this article, you have learned how to convert multiple columns of pandas DataFrame to DateTime using pandas.to_datetime()
, astype()
and DataFrame.apply()
function. Using these you can convert data from string, object, and int columns to DateTime format.
Happy Learning !!
Related Articles
- Pandas Convert String Column To DateTime
- Convert List of Dictionaries to Pandas DataFrame
- Sum Pandas DataFrame Columns With Examples
- How to Print Pandas DataFrame without Index
- Check If a Column Exists in Pandas DataFrame
- Pandas DatetimeIndex with Examples
- Pandas Convert Column to Numpy Array
- Pandas Convert Date (datetime) to String Format
- Pandas Convert Datetime to Date Column