I am trying to Convert yy-mm-dd to Q1 , Q2 for each company ID.
Hi community,
I am working with institutional ownership data and I extracting quarterly historical investor data. I want to convert my dates for each company to quarterly format as is shown in the attached picture below.
My current data looks like this:
But I want it in the format as shown
I have had a look at @Alan Tam123 post https://community.developers.refinitiv.com/questions/60189/date-format-and-column-headings.html but the code seems to be different from how I am doing it.
Any help would be great! @Jirapongse look forward to your expert help.
My code is attached below:
df,e = ek.get_data(instruments=['AAPL.O','AAL.L'], fields=[
'TR.CommonName',
'TR.InvestorFullName("TheInvestorType":"113,108,107")',
'TR.SharesHeld.calcdate',
'TR.SharesHeld',
'TR.PctOfSharesOutHeld',
'SUM(TR.PctOfSharesOutHeld)',
'TR.InvestorType',
'TR.InvestorTypeId'],parameters={'SDate':'0','EDate':'-4', 'Period':'FQ0','Frq':'FQ'})
df
#EDate is the number of quaters
df1= df
df1['id']=df1.groupby(['Instrument']).ngroup()
df1.set_index(['id','Calc Date'])
Best Answer
-
I am not a Python Dataframe expert so I used the for loop to check and change the values.
First, I added the Quarter and QuaterTemp columns to the data frame.
df,e = ek.get_data(instruments=['AAPL.O','AAL.L'], fields=[
'TR.CommonName',
'TR.InvestorFullName("TheInvestorType":"113,108,107")',
'TR.SharesHeld.calcdate',
'TR.SharesHeld',
'TR.PctOfSharesOutHeld',
'SUM(TR.PctOfSharesOutHeld)',
'TR.InvestorType',
'TR.InvestorTypeId'],parameters={'SDate':'0','EDate':'-4', 'Period':'FQ0','Frq':'FQ'})
df['Quarter'] = pd.PeriodIndex(df['Calc Date'], freq='Q')
df['QuarterTemp'] = df['Quarter']
dfThen, I used a for loop to change the values in the Quarter column according to the following conditions. Next, I dropped the QuarterTemp column.
for i in range(1, len(df)):
if ((df.loc[i-1,'QuarterTemp'] == df.loc[i,'QuarterTemp']) and (df.loc[i-1,'Instrument'] == df.loc[i,'Instrument'])):
df.loc[i, 'Quarter'] = ""
df = df.drop('QuarterTemp', axis=1)
dfFinally, I called the groupby and set_index methods.
df1= df
df1['id']=df1.groupby(['Instrument']).ngroup()
df1 = df1.set_index(['id','Quarter'])
df12
Answers
-
Thank you for reaching out to us.
Please try this code:
df,e = ek.get_data(instruments=['AAPL.O','AAL.L'], fields=[
'TR.CommonName',
'TR.InvestorFullName("TheInvestorType":"113,108,107")',
'TR.SharesHeld.calcdate',
'TR.SharesHeld',
'TR.PctOfSharesOutHeld',
'SUM(TR.PctOfSharesOutHeld)',
'TR.InvestorType',
'TR.InvestorTypeId'],parameters={'SDate':'0','EDate':'-4', 'Period':'FQ0','Frq':'FQ'})
df['Quarter'] = pd.PeriodIndex(df['Calc Date'], freq='Q')
dfThen, run this code.
df1= df
df1['id']=df1.groupby(['Instrument']).ngroup()
df1.set_index(['id','Quarter'])2 -
Thank you ! It works fine. I want to know if its is possible that instead of the quarters repeating for each ID , it should just stay once per quarter. Please see attached. @Jirapongse
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
- 60 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛