Home > Back-end >  time data 'June 13, 1980 (United States)' does not match format '%m/%d/%Y' (matc
time data 'June 13, 1980 (United States)' does not match format '%m/%d/%Y' (matc

Time:06-26

How can i passs a datetime format on a column with str such as June 13, 1980 (United States)

i tried df['format_released'] = pd.to_datetime(df['released'], format='%m/%d/%Y')

got this error

time data 'June 13, 1980 (United States)' does not match format '%m/%d/%Y' (match)

CodePudding user response:

The correct format is: pd.to_datetime(pd.to_datetime(df['released'], format='%B %d, %Y')

For the full name, you need to specify %B for the format. You don't need the value "(United States)" in the string.

CodePudding user response:

You need to preprocess the column to discard the non relevant data.

Using str.replace:

df['format_released'] = pd.to_datetime(df['released'].str.replace(r'\s*(.*$', '', regex=True), format='%B %d, %Y')

Or using str.extract:

df['format_released'] = pd.to_datetime(df['released'].str.extract(r'(\w  \d , \d )', expand=False), format='%B %d, %Y')
  • Related