Today, we are going
to explore a interesting and important scenario in data analysis using Python.
At times, it becomes necessary to convert various date
formats into a uniform format while working with file data (CSV/Excel)
processing.
It can be quite challenging to identify the date formats
present in each column, and performing individual conversions for each column
can be time-consuming. Moreover, if the original date formats in the source
columns are altered, it may result in errors.
To address this issue, we have devised a Python code that can
dynamically convert different date formats into a single, standardized format.
All you have to do is include the required date formats
in the date format list provided in the code.
This approach will help streamline the process and ensure
consistency in your data analysis tasks.
from datetime import datetime
import pandas as pd
from calendar import monthrange
def convert_date(input_date):
input_date = str(input_date)
date_formats_1 = ["%d/%m/%Y","%d/%B/%y","%d/%b/%y","%d/%m/%y","%d-%m-%Y","%d-%b-%y","%d-%m-%y"]
date_formats_2 = ["%m-%y", "%m-%Y", "%m/%Y","%B-%Y","%b-%Y","%b-%y"]
for date_format in date_formats_1:
try:
#date_obj = datetime.strptime(input_date, date_format)
#return date_obj.strftime("%Y-%m-%d")
return datetime.strptime(input_date, date_format).strftime("%Y-%m-%d")
except ValueError:
pass
for date_format in date_formats_2:
try:
date_object = datetime.strptime(input_date, date_format)
last_day_of_month = monthrange(date_object.year,date_object.month)[1]
new_date = date_object.replace(day=last_day_of_month).strftime("%Y-%m-%d")
#return date_object.strftime("%Y-%m-%d")
return new_date
except ValueError:
pass
return input_date
# Test the function
def read_file(file):
df = pd.read_csv(file)
print(df)
# Iterate through columns and apply date conversion if it's a date column
for column in df.columns:
df[column] = df[column].apply(convert_date)
print( df)
input_file = 'G:\ETL_Automation\data\diff_date.csv'
read_file(input_file)
Please make any necessary updates to the code, and if you have any comments or recommendations, please share them.
If you provide the above code with additional dynamism or more advancement , that would be great.
0 Comments
Thanks for your message.