transform '30-06-2022' to '2022-06-30' in pd.datetime in python

To transform a date string from '30-06-2022' to '2022-06-30' using pd.to_datetime() function in Python, you can specify the format of the input date using the format parameter. Here's how you can do it:

main.py
import pandas as pd

date_string = '30-06-2022'
date_transformed = pd.to_datetime(date_string, format='%d-%m-%Y').strftime('%Y-%m-%d')

print(date_transformed)
160 chars
7 lines

Output:

main.py
2022-06-30
11 chars
2 lines

In the code above, the %d corresponds to the day, %m corresponds to the month, and %Y corresponds to the year in the input date string. The strftime('%Y-%m-%d') function is used to convert the transformed date into the desired format.

Note: Make sure to include the necessary import statement to use the pd.to_datetime() function from the Pandas library.

related categories

gistlibby LogSnag