pythonpandasdatetime-format

Different Date Formats In Same Column


I've a dataset having a column: Joining_date with different date formats. I want to convert them in a one same date format

df['Joining_Date']
0        2016-10-26
2        16/08/2018
3        2017/06/28
6        2021-05-29
9          01-28-13
12       2021-05-26
13       2018-04-06
14      19-Jul-2023
15      23-Nov-2013
19      13-Jun-2011
22       2011/04/27
27       2017/01/06
31         12-16-17
39       2022/07/10
40       2022-11-25
42       05/12/2022
43      09-Oct-2016

Solution

  • You can use parameter format='mixed' with the caveat from the docs.

    "mixed", to infer the format for each element individually. This is risky, and you should probably use it along with dayfirst.

    pd.to_datetime(df['Joining_Date'], format='mixed')
    

    Output:

    0    2016-10-26
    1    2018-08-16
    2    2017-06-28
    3    2021-05-29
    4    2013-01-28
    5    2021-05-26
    6    2018-04-06
    7    2023-07-19
    8    2013-11-23
    9    2011-06-13
    10   2011-04-27
    11   2017-01-06
    12   2017-12-16
    13   2022-07-10
    14   2022-11-25
    15   2022-05-12
    16   2016-10-09
    Name: 1, dtype: datetime64[ns]
    

    Update: OP solved their issue using:

    pd.to_datetime(df['Joining_Date'], format='mixed', datefirst=True)