Search code examples
python-3.xpandas

Pandas list dates to datetime


I am looking to convert a column with dates in a list [D, M, Y] to a datetime column. The below works but there must be a better way?

new_df = pd.DataFrame({'date_parts': [[29, 'August', 2024], [28, 'August', 2024], [27, 'August', 2024]]})
display(new_df)

## Make new columns with dates
new_df = pd.concat([new_df, new_df['date_parts'].apply(pd.Series)], axis=1).rename(columns={0:'D', 1:'M', 2:'Y'})

month_map = {
'January':1,
'February':2,
'March':3,
'April':4,
'May':5,
'June':6,
'July':7,
'August':8,
'September':9,
'October':10,
'November':11,
'December':12
}

## make datetime column
new_df['release_date'] = pd.to_datetime(dict(year=new_df.Y, month=new_df.M.apply(lambda x: month_map[x]), day=new_df.D),  format='%d-%B-%Y') 
new_df.drop(columns=['D', 'M', 'Y'])
## Input
    date_parts
0   [29, August, 2024]
1   [28, August, 2024]
2   [27, August, 2024]

## Output
    date_parts          release_date
0   [29, August, 2024]  2024-08-29
1   [28, August, 2024]  2024-08-28
2   [27, August, 2024]  2024-08-27

Solution

  • Just combine the parts into a single string, and pass to to_datetime:

    new_df['release_date'] = pd.to_datetime(new_df['date_parts']
                                            .apply(lambda x: '-'.join(map(str, x))),
                                            format='%d-%B-%Y')
    

    Output:

               date_parts release_date
    0  [29, August, 2024]   2024-08-29
    1  [28, August, 2024]   2024-08-28
    2  [27, August, 2024]   2024-08-27
    

    You could also convert the list of list to DataFrame with day/month/year columns:

    new_df['release_date'] = pd.to_datetime(
        pd.DataFrame(
            new_df['date_parts'].to_list(),
            index=new_df.index,
            columns=['day', 'month', 'year'],
        ).replace({'month': month_map})
    )