[Python] Pandas list dates to datetime

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página