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. @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
- 6 AHS
- 39 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛