You could apply a custom function on your Date column to parse the date values.
import pandas as pd
import io
from datetime import datetime
temp_data=u"""Date
8/1/2021
8-5-2021
8-6-2021:08:00:00 PM
"""
data = pd.read_csv(io.StringIO(temp_data), sep=";", parse_dates=False)
def to_date(string_date):
formats=["%d/%m/%Y","%d-%m-%Y","%d-%m-%Y:%I:%M:%S %p"]
parsed_date=None
for format in formats:
try:
parsed_date=datetime.strptime('8-6-2021:08:00:00 PM', "%d-%m-%Y:%I:%M:%S %p").date()
return parsed_date
except ValueError:
pass
raise RuntimeError(f"Unable to parse date {string_date} with available formats={formats}")
You can add new formats in the to_date function to parse any new date format.
data['Date']=data['Date'].apply(lambda row: to_date(row))
>>> data
Date
0 2021-06-08
1 2021-06-08
2 2021-06-08
.applying over the series with the dates. You can then convert the PendulumDateTimeobjects back to "plain"datetime.datetimeobjects, or emit strings in a consistent format, etc.