Search code examples
pythonpandasnumpydatetimestring-to-datetime

Convert string to date in python if date string has different format


My data has date variable with two different date formats

Date
01 Jan 2019
02 Feb 2019
01-12-2019
23-01-2019
11-04-2019
22-05-2019

I want to convert this string into date(YYYY-mm-dd)

Date
2019-01-01
2019-02-01
2019-12-01
2019-01-23
2019-04-11
2019-05-22

I have tried following things, but I am looking for better approach

df['Date'] = np.where(df['Date'].str.contains('-'), pd.to_datetime(df['Date'], format='%d-%m-%Y'), pd.to_datetime(df['Date'], format='%d %b %Y'))

Working solution for me

df['Date_1']= np.where(df['Date'].str.contains('-'),df['Date'],np.nan)
df['Date_2']= np.where(df['Date'].str.contains('-'),np.nan,df['Date'])
df['Date_new'] = np.where(df['Date'].str.contains('-'),pd.to_datetime(df['Date_1'], format = '%d-%m-%Y'),pd.to_datetime(df['Date_2'], format = '%d %b %Y'))

Solution

  • Just use the option dayfirst=True

    pd.to_datetime(df.Date, dayfirst=True)
    
    Out[353]:
    0   2019-01-01
    1   2019-02-02
    2   2019-12-01
    3   2019-01-23
    4   2019-04-11
    5   2019-05-22
    Name: Date, dtype: datetime64[ns]