[Solved] TypeError: replace() takes no keyword arguments on changing timezone

I’m trying to change the timezone of UTC to America/Sao Paulo, but I’m getting this error:

TypeError: replace() takes no keyword arguments

This is my code:

import pytz

local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = candles[0]['time'].replace(tzinfo=pytz.utc).astimezone(local_tz)

Candle time is:

>>> candles[0]['time']
u'2017-08-03T00:03:00.000000Z'

How can I fix this?

Solution #1:

You need to convert your candles[0]['time'] which is a unicode string to datetime object.

Here is an example:

import datetime, pytz

a = u'2017-08-03T00:03:00.000000Z'
local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = datetime.datetime.strptime(a, '%Y-%m-%dT%H:%M:%S.000000Z').replace(tzinfo=pytz.utc).astimezone(local_tz)
print local_dt

Output:

2017-08-02 21:03:00-03:00
Respondent: Chiheb Nexus

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.