I have a Python DataFrame with a datetime column that has inconsistent format, and would like it to be all one format. The DataFarme contains 199622 rows, so this is not an exhaustive sample:
Example of DataFrame Column as an object type:
Date 0 6/14/22 1 6/1/22 2 5/23/22 3 11/17/21 4 11/9/21 5 May 4, 2021 6:02 PM 6 April 29, 2021 10:06 AM 7 March 31, 2021 2:04 PM ...
The desired transformed output would be a DateFrame with a date type column formatted:
Date 0 2022-06-14 1 2022-06-01 2 2022-05-23 3 2021-11-17 4 2021-11-09 5 2021-05-04 6 2021-04-29 7 2021-03-31
Is it possible to create a function that does this transformation for both sets of formats in a single column?
Advertisement
Answer
This should work.
df['Date'] = pd.DatetimeIndex(df['Date']) df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')