Filipe Ferminiano
Filipe Ferminiano

Reputation: 8801

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?

Upvotes: 6

Views: 22747

Answers (1)

Chiheb Nexus
Chiheb Nexus

Reputation: 9267

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

Upvotes: 9

Related Questions