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.
JavaScript
x
5
1
# Convert to date
2
mood['response_time'] = pd.to_datetime(mood['response_time'], utc=True)
3
# Remove +01:00
4
mood['response_time'] = mood['response_time'].dt.strftime('%Y-%m-%d %H:%M:%S')
5
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):
JavaScript
1
7
1
mood['response_time'] = '2019-02-21 15:31:37+01:00'
2
# Convert to date
3
mood['response_time'] = pd.to_datetime(mood['response_time'])
4
# Remove +01:00
5
mood['response_time'] = mood['response_time'].strftime('%Y-%m-%d %H:%M:%S')
6
# -> '2019-02-21 15:31:37'
7