Skip to content
Advertisement

Remove timezone (+01:00) from DateTime

I would like to delete the timezone from my dateTime object.
Currently i have:
2019-02-21 15:31:37+01:00

Expected output:
2019-02-21 15:31:37

The code I have converts it to: 2019-02-21 14:31:37.

# Convert to date
mood['response_time'] = pd.to_datetime(mood['response_time'], utc=True)
# Remove +01:00
mood['response_time'] = mood['response_time'].dt.strftime('%Y-%m-%d %H:%M:%S')

Advertisement

Answer

In the first line, the parameter utc=True is not necessary as it converts the input to UTC (subtracting one hour in your case).

In the second line, I get an AttributeError: 'Timestamp' object has no attribute 'dt'. Be aware that to_datetime can return different objects depending on the input.

So the following works for me (using a Timestamp object):

mood['response_time'] = '2019-02-21 15:31:37+01:00'
# Convert to date
mood['response_time'] = pd.to_datetime(mood['response_time'])
# Remove +01:00
mood['response_time'] = mood['response_time'].strftime('%Y-%m-%d %H:%M:%S')
# -> '2019-02-21 15:31:37'
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement