Pandas is a popular Python library for analyzing and manipulating data. Pandas users often encounter the problem of converting strings to datetime objects, which can be used for various time series operations.
Depending on the format of the strings and the number of columns to convert, there are different methods to convert strings to datetime in pandas data frames.
This blog post discusses these methods using a variety of examples:
Method 1: Using pandas.to_datetime() Function
The “pandas.to_datetime()” function of the Pandas module is used to convert the string representation of date and time into a Python datetime object. The syntax of the function is shown below:
Syntax:
pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False)
The “pandas.to_datetime()” function has many parameters that can control how the input is parsed and converted. For instance, the parameter “utc=True” retrieves a timezone UTC-localized object, and the parameter “infer_datetime_format=True” detects the input format automatically.
Here is an example of the conversion of strings to datetime:
Example 1: Convert Strings to Datetime in Pandas
The below example shows the conversion of strings to datetime in Pandas:
Code:
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['20210305','20210316','20210328']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%Y%m%d')
print (data_frame)
- The module named “pandas” is imported.
- The dictionary data is initialized and stored in a variable named “data.”
- The “pd.DataFrame()” function creates the DataFrame by accepting the dictionary data.
- The specified columns “Admission_Dates” containing strings datetime is converted into Python datetime using the “pd.to_datetime()” function.
- The specific format of the string’s datetime is passed as a parameter value to the “pd.to_datetime()” function.
Output:
The specified column containing the string’s datetime has been converted into Python datetime.
Example 2: Convert Strings to Datetime in Pandas Using Different Format
The given below code convert the strings to datetime in Pandas using various format code:
Code: Using ‘%d%m%Y’ Format
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['05032021','16032021','28032021']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%d%m%Y')
print (data_frame)
- The Pandas DataFrame column “Admission_Dates” contains the string dates in “%d%m%y” format.
- The “pandas.to_datetime()” function takes the specified column name and datetime format “%d%m%y” to convert the string’s datetime into Python datetime.
Output:
The string’s datetime has been converted into datetime based on the specified DateTime format.
Code: Using ‘%d%b%Y’ Format
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['05Mar2022','16Mar2022','28Feb2022']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%d%b%Y')
print (data_frame)
- The Pandas DataFrame column “Admission_Dates” contains the string dates in “05Mar2022” format.
- To convert this type of string representation of datetime the “%d%b%Y” format is passed to the “pandas.to_datetime()” function.
Output:
The string values have been converted into Python datetime.
Code: Using ‘%d-%b-%Y’ Format
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['05-Mar-2022','16-Mar-2022','28-Feb-2022']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%d-%b-%Y')
print (data_frame)
- The specific column of the given data frame contains the string’s datetime representation with dashes such as “05-Mar-2022”.
- The specific format “%d-%b-%Y” is passed to the “pd.to_datetime()” function to convert/transform the strings to Datetime.
Output:
The specified strings” values have been converted into Python datetime.
Example 3: Convert Timestamps Strings to Datetime in Pandas
The below example is used to convert timestamps strings to datetime:
Code:
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['20221225093000','20221226093000','20221218200000']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%Y%m%d%H%M%S')
print (data_frame)
- The timestamps strings are initialized in the DataFrame column named “Admission_Dates”.
- The “pd.to_datetime()” function takes the column name and formats “%Y%m%d%H%M%S” to convert the strings to Datetime.
Output:
The datetime strings have been converted into Python datetime.
Example 4: Change Multiple Strings Column to Datetime in Pandas
The given below code is used to change the multiple strings column to datetime:
Code:
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['20210305','20210316','20210328'],
'Orientation_Dates': ['20210315','20210326','20210408']}
data_frame = pandas.DataFrame(data)
data_frame['Admission_Dates'] = pandas.to_datetime(data_frame['Admission_Dates'], format='%Y%m%d')
data_frame['Orientation_Dates'] = pandas.to_datetime(data_frame['Orientation_Dates'], format='%Y%m%d')
print (data_frame)
- The Pandas DataFrame contains two string columns “Admission_Dates” and “Orientation_Dates”.
- The “pandas.to_datetime()” function is used twice to convert each of the multiple string columns of the data frame to a Python datetime object.
- The format for the datetime argument is passed to the “pandas.to_datetime()” function.
Output:
Method 2: Using DataFrame.astype() Function
The “dataframe.astype()” function casts pandas objects to a specified data type. It can be used to change the type of multiple columns in a data frame. Below is the syntax of the function:
dataframe.astype(dtype, copy=True, errors='raise')
In the above syntax, the parameter “dtype” can be a numpy.dtype or Python type, or a dictionary of column names and data types, etc. let’s understand its working using the following example:
Code:
import pandas
data = {'Students':['Joseph', 'Alex', 'Lily'],
'Admission_Dates': ['20210305','20210316','20210328']}
data_frame = pandas.DataFrame(data)
data_frame["Admission_Dates"] = data_frame["Admission_Dates"].astype('datetime64[ns]')
print (data_frame)
The “df.astype()” function takes the “datetime64[ns]” object to convert the given strings datetime object into Python datetime object.
Output:
Datetime objects have been created from the given strings.
Conclusion
To convert strings to datetime in Pandas DataFrame, the “pandas.to_datetime()” function, and the “DataFrame.astype()” function is used in Python. The “pandas.to_datetime()” function converts strings to datetime using a different format. Timestamps can also be converted to datetimes with this function in Pandas.
This post presented various examples to demonstrate how to convert strings to datetime in a Pandas DataFrame.