'How to change dates in string form to datetime format? [closed]

I have a dataframe that contains dates in string format and I am trying to change the dates to a datetime format. The format the dates are currently in is "Month Day, Year" with no leading zero on the day, e.g. 'May 1, 2014'.

I have tried a couple of methods below but I keep getting a value error when I use the %-d to represent no leading zeros:


parser = lambda x: dt.datetime.strptime(x, '%b %-d %Y')

# Original Kaggle Dataset
employee_df = pd.read_csv('employee_reviews.csv', 
                          encoding='ISO-8859-1', 
                          skipinitialspace=True, 
                          parse_dates=['dates'], 
                          date_parser=parser
                         )
# 2nd solution

employee_df = pd.read_csv('employee_reviews.csv', 
                          encoding='ISO-8859-1', 
                          skipinitialspace=True, 
                          parse_dates=['dates'], 
                          date_parser=pd.to_datetime('%b %-d %Y'))

This is the error I am getting: enter image description here



Solution 1:[1]

Update the parser with the following line of code

parser = lambda x: dt.datetime.strptime(x, '%b %d, %Y')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Zahid Raja