Convert String "YYYY-MM-DD hh:mm:ss Etc/GMT" to timestamp in UTC pandas

Question:

I have a pandas column of datetime-like string values like this exampe:

exammple_value = "2022-06-24 16:57:33 Etc/GMT"

Expected output

Timestamp('2022-06-24 16:57:33+0000', tz='UTC')

Etc/GMT is the timezone, you can get it in python with:

import pytz
list(filter(lambda x: 'GMT' in x, pytz.all_timezones))[0]

----
OUT: 
'Etc/GMT'
Asked By: Leo

||

Answers:

Use to_datetime with %Z for parse timezones and for UTC use Timestamp.tz_convert:

exammple_value = "2022-06-24 16:57:33 Etc/GMT"

print (pd.to_datetime(exammple_value, format='%Y-%m-%d %H:%M:%S %Z').tz_convert('UTC'))
2022-06-24 16:57:33+00:00

Another idea is remove timezones by split:

print (pd.to_datetime(exammple_value.rsplit(maxsplit=1)[0]).tz_localize('UTC'))
2022-06-24 16:57:33+00:00
Answered By: jezrael