I wrote a function that converts all the dates of the specified date column to the specified format. Any missing or invalid date is replaced with a value specified by the user.
The issue is, all the dates are being converted to the same default format '%Y/%m/%d', regardless of the format specified.
What I have tried:
Code:
# Date Formatting
from dateutil.parser import parse
import pandas as pd
def date_fun(dfd, date_inputs):
try:
col_name = date_inputs["DateColumn"]
replace_date = date_inputs["ReplaceValue"]
date_col = dfd[col_name]
formatted_dates = pd.to_datetime(date_col, errors='coerce')
# dayfirst=True
# formatted_dates = pd.to_datetime(date_col, errors='coerce', dayfirst=True)
# formatted_dates = pd.to_datetime(date_col, format='%m/%d/%Y', errors='coerce')
# formatted_dates = pd.to_datetime(date_col, format='%Y/%m/%d', errors='coerce')
# formatted_dates = pd.to_datetime(date_col, format='%d/%m/%Y', errors='coerce')
if replace_date is not None:
formatted_dates = formatted_dates.fillna(replace_date)
dfd[col_name] = formatted_dates
result_dict = dfd.to_dict(orient='records')
for record in result_dict:
for key, value in record.items():
if isinstance(value, float):
record[key] = str(value)
return result_dict
except Exception as e:
logging.error(f"An error occurred while performing the operation: {e}")
Column Data
25.09.2019
9/16/2015
10.12.2017
2008,13,02
23-01-1992, 7:45
My Output
2019-09-25T00:00:00
2015-09-16T00:00:00
2017-10-12T00:00:00
2008-02-01T00:00:00
1992-01-23T07:45:00
Why is the conversion of dates to the specified format not happening?
NOTE: I kept the different formats that I tried in comments.