Search code examples
pythonpandasdatedate-format

Convert date in pandas series


I need help: pandas dataset has a date column. The date in one of the columns is in the format "September 25, 2021 ". I can't convert this to "yyyy-mm-dd" format (2021-09-25). This is necessary in order to import this data into mysql in the future (I work through dbever). Might be a stupid question but I'm a newbie

Tried to use to_datetime function and this way (IndexError: list index out of range):


date = {'January': '01', 'February': '02', 'March': '03', 'April': '04', 'May': '05', 'June': '06', 
        'July': '07', 'August': '08', 'September': '09', 'October': '10', 'November': '11', 'December':     '12'}
new_date = []
for d in df['date_added']:
    month = d.split(' ')[0]
    day = d.split(' ')[1]
    year = d.split(', ')[2]
    res = year.split('-') + date[month].split('-') + day
    new_date.append(res)

Solution

  • import pandas as pd
    
    df = pd.DataFrame({'date_added': ['September 25, 2010', 'April 1, 2023']})
    
    r = pd.to_datetime(df['date_added'], format='%B %d, %Y')
    
    print(r)
    

    Result

    0   2010-09-25
    1   2023-04-01
    Name: date_added, dtype: datetime64[ns]