Issue
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)
Solution
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')
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.