I'm getting an error when trying to parse datetime:
ValueError: ("time data '2019-07-05T10:19:42+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'", 'occurred at index 0')
the line that is generating this error is:
df['yearmonth'] = df.apply(lambda row: datetime.strptime(row['createdAt'], '%Y-%m-%dT%H:%M:%S%z').strftime("%Y%m"), axis=1)
As far as I can see the format is correct, and the code runs fine on other machines.
It works when I remove the %z from the format and just take a slice of the data to be formatted, for example as below:
df['yearmonth'] = df.apply(lambda row: datetime.strptime(row['createdAt'][:19], '%Y-%m-%dT%H:%M:%S').strftime("%Y%m"), axis=1)
So I'm guessing that %z is causing issues. I'm figuring that the issue is to do with locales but I've tried setting locale in all different sorts of ways without success.
Would really appreciate some insight on how to fix this without using the slice!
While parsing of '%z' is supported by Python 3.6, when in this format:
+HHMM or -HHMM
it does not support parsing of the '%z' if this section of the date includes a colon:
+HH:MM or -HH:MM
However, Python 3.7 does support parsing of the '%z' section when there is a colon in this section.
Therefore, using Python 3.7 instead of Python 3.6 will resolve this issue.