Pandas program to convert a string of date into time


In this program, we will convert a date string like "24 August 2020" to 2020-08-24 00:00:00. We will use the to_datetime() function in pandas library to solve this task.

Algorithm

Step 1: Define a Pandas series containing date string.
Step 2: Convert these date strings into date time format using the to_datetime format().
Step 3: Print the results.

Example Code

import pandas as pd

series = pd.Series(["24 August 2020", "25 December 2020 20:05"])
print("Series: \n", series)

datetime = pd.to_datetime(series)
print("DateTime Format: \n", datetime)

Output

Series:
0            24 August 2020
1    25 December 2020 20:05
dtype: object
DateTime Format:
0   2020-08-24 00:00:00
1   2020-12-25 20:05:00
dtype: datetime64[ns]

Updated on: 16-Mar-2021

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements