question

Upvotes
Accepted
1 1 1 1

How to convert the timezone of the API data result from UTC to IST?

For an external client, she is using the below code to get historical data using Phyton from EIkon

ek.set_app_key('***')

data=ek.get_timeseries('SBI.NS', interval='tick')

data.tail(10)


output

SBI.NS VALUE VOLUME

Date

2021-07-09 10:20:09.000 423.75 6

2021-07-09 10:20:21.000 423.75 10

2021-07-09 10:20:24.000 423.75 15

2021-07-09 10:20:37.000 423.75 1

2021-07-09 10:20:44.000 423.75 300

2021-07-09 10:21:39.000 423.75 1

2021-07-09 10:26:15.000 423.75 1


The timestamp is in UTC. She would like to know if there is a way she can get the timestamp of the results in IST.

eikoneikon-data-apiworkspacerefinitiv-dataplatform-eikonworkspace-data-apitime-zone
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
39.4k 78 11 27

@jonathan.legrand

The pythonic way is to utilize vectorized methods wherever possible instead of your own loops. The main reason is that vectorized methods are optimized for runtime and typically run faster than your own loops (sometimes by an order of magnitude faster). Vectorized methods also improve readability of the code by making the code more compact.

@jaredd.matutina01

Please see an example of of converting the timezone in the dataframe index from UTC to the timezone of your choice on this thread.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

This is a very good point, maybe this is better for you @jaredd.matutina01 then:


import pytz

data.index = data.index.tz_localize(pytz.utc).tz_convert('Asia/Kolkata')
Upvotes
7.1k 21 3 6

Hi @jaredd.matutina01 ,


I would suggest looking this up on StackOverflow:

https://stackoverflow.com/questions/4770297/convert-utc-datetime-string-to-local-datetime


Does this provide the answer you're looking for?


E.g.:


import datetime
from pytz import timezone
ist = []
for i in data.index:
    try:
        _ist = datetime.datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S.%f')
        _ist = _ist.astimezone(timezone('Asia/Kolkata'))
        _ist = _ist.strftime("%Y-%m-%d %H:%M:%S.%f")
    except:
        _ist = datetime.datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S')
        _ist = _ist.astimezone(timezone('Asia/Kolkata'))
        _ist = _ist.strftime("%Y-%m-%d %H:%M:%S")
    ist.append(_ist)
data.index = ist
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@jonathan.legrand @jaredd.matutina01

I ran the above code but the data index remained the same. This is what I ran:

ist = []

for i in data.index:

try:

_ist = datetime.datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S.%f')

_ist = _ist.astimezone(timezone('Asia/Kolkata'))

_ist = _ist.strftime("%Y-%m-%d %H:%M:%S.%f")

except:

_ist = datetime.datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S')

_ist = _ist.astimezone(timezone('Asia/Kolkata'))

_ist = _ist.strftime("%Y-%m-%d %H:%M:%S")

ist.append(_ist)

data.index = ist

data.tail()

but the index remained utc

Upvotes
1 1 1 1

Thank you guys for these inputs!


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
3 1 2 5

@jaredd.matutina01 @Alex Putkov. @jonathan.legrand

Hi, I tried this code but got the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-bcd1c96da781> in <module>
      1 import pytz
      2 ----> 3 data.index = data.index.tz_localize(pytz.utc).tz_convert('Asia/Kolkata')

AttributeError: 'Index' object has no attribute 'tz_localize
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi @akshmita ,


That will be because the Python objects used as indices in your pandas data-frame (if indeed 'data ' is a pandas data-frame in your code) are not of type 'pandas.core.indexes.datetimes.DatetimeIndex'. Would you mind either (i) showing us more of your code that leads to your 'data ' object or (ii) the object type of your index? Without this information, we will not be able to help.

Upvotes
3 1 2 5

@jonathan.legrand here's the full code

import eikon as ek

import datetime as dt

from datetime import time

import pytz


ek.set_app_key('.....')

data=ek.get_timeseries('SBI.NS', interval='tick')

data.index = data.index.tz_localize(pytz.utc).tz_convert('Asia/Kolkata')

data.tail()

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@akshmita

I have no problem running this code on my end. Could you check whether you get any values in the dataframe? I'm thinking that perhaps for some reason get_timeseries method does not return any timeseries for you. Comment out the line that converts the timezone for the index and run the code to see what's returned into the dataframe. If the dataframe looks ok to you, check the type for the index. Run

type(data.index)

The expected results is pandas.core.indexes.datetimes.DatetimeIndex. What do you get?

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.