This recently happened to me, I had a spreadsheet with a column full of different date formats? I learned that you can handle this easily if you’re using Pandas 2.0.0 or greater!
Below is an example of how easy it is to convert the column to a datetime object and then convert it back to a string in a standardized format.
df['Date_of_Birth'] = pd.to_datetime(df['Date_of_Birth'], format='mixed')df['Date_of_Birth'] = df['Date_of_Birth'].dt.strftime('%Y-%m-%d')
That’s it! Two lines of code to standardize any date column. For more information on the format='mixed' parameter and other datetime options, see the pandas.to_datetime() reference.
Enjoy!
Leave a comment