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.
Best Answer
-
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.
Please see an example of of converting the timezone in the dataframe index from UTC to the timezone of your choice on this thread.
0
Answers
-
Hi @jaredd.matutina01 ,
I would suggest looking this up on StackOverflow:
https://stackoverflow.com/questions/4770297/convert-utc-datetime-string-to-local-datetimeDoes this provide the answer you're looking for?
E.g.:
import datetime
from pytz import timezoneist = []
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
0 -
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')0 -
Thank you guys for these inputs!
0 -
@jaredd.matutina01 @Alex Putkov.1 @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_localize0 -
@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
0 -
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.
0 -
@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()
0 -
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?
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛