import refinitiv.data as rd
rd.open_session()
rd.discovery.search(
view=rd.discovery.Views.MUNICIPAL_INSTRUMENTS,
top=10,
filter="(IsActive eq true and SearchAllCategoryv2 eq 'US Municipal Bonds' and (((((((((((((((((((((((RatingsScope((RatingType eq 'FTC'))) or (RatingsScope((RatingType eq 'FUN')))) or (RatingsScope((RatingType eq 'FST')))) or (RatingsScope((RatingType eq 'FUS')))) or (RatingsScope((RatingType eq 'FDR')))) or (RatingsScope((RatingType eq 'FRR')))) or (RatingsScope((RatingType eq 'MDY')))) or (RatingsScope((RatingType eq 'MUN')))) or (RatingsScope((RatingType eq 'MLI')))) or (RatingsScope((RatingType eq 'MLH')))) or (RatingsScope((RatingType eq 'MST')))) or (RatingsScope((RatingType eq 'MSH')))) or (RatingsScope((RatingType eq 'MUS')))) or (RatingsScope((RatingType eq 'MSI')))) or (RatingsScope((RatingType eq 'MLN')))) or (RatingsScope((RatingType eq 'FDL')))) or (RatingsScope((RatingType eq 'FDS')))) or (RatingsScope((RatingType eq 'MDT')))) or (RatingsScope((RatingType eq 'MDR')))) or (RatingsScope((RatingType eq 'MIS')))) or (RatingsScope((RatingType eq 'MSS')))))))",
select="LongName,Ticker,CouponRate,MaturityDate,CUSIP,RIC,MuniStateDescription,CouponClassDescription,EJVAssetID,BusinessEntity,PI,SearchAllCategoryv3,SearchAllCategoryv2,SearchAllCategory,IssueDate,BankQualified,BondInsuranceDescription,IsCallable,IsPutable,IsSinkable,RatingsScope(filter:((RatingType xeq 'FTC') or (RatingType xeq 'FUN') or (RatingType xeq 'FST') or (RatingType xeq 'FUS') or (RatingType xeq 'FDR') or (RatingType xeq 'FRR') or (RatingType xeq 'MDY') or (RatingType xeq 'MUN') or (RatingType xeq 'MLI') or (RatingType xeq 'MLH') or (RatingType xeq 'MST') or (RatingType xeq 'MUS') or (RatingType xeq 'MSH') or (RatingType xeq 'MSI') or (RatingType xeq 'MLN') or (RatingType xeq 'FDL') or (RatingType xeq 'FDS') or (RatingType xeq 'MDT') or (RatingType xeq 'MDR') or (RatingType xeq 'MIS') or (RatingType xeq 'MSS'))),RatingX1XRatingRank"
)
When I run this code in Codebook, the result is a single field, RatingScope Column, containing multiple dictionaries grouped by RatingType.
For example, a cell in RatingScope contains the following data:
[{'Agency': 'MDY', 'CurrentRating': 'A1', 'CurrentRatingDate': '2023-10-03T00:00:00.000Z', 'CurrentRatingRank': 5, 'Preliminary': 'n', 'RatingScope': 'D', 'RatingType': 'MDT', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'Aa3', 'CurrentRatingDate': '2020-04-23T00:00:00.000Z', 'CurrentRatingRank': 4, 'Preliminary': 'n', 'RatingScope': 'D', 'RatingType': 'MDR', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'MIG 1', 'CurrentRatingDate': '2024-06-17T00:00:00.000Z', 'CurrentRatingRank': 1, 'Preliminary': 'n', 'PreviousRatingDate': '2023-10-03T00:00:00.000Z', 'PreviousRatingRank': 1, 'RatingType': 'MUS', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'MIG 1', 'CurrentRatingDate': '2024-06-17T00:00:00.000Z', 'CurrentRatingRank': 1, 'Preliminary': 'n', 'PreviousRatingDate': '2023-10-03T00:00:00.000Z', 'PreviousRatingRank': 1, 'RatingType': 'MST', 'Solicited': 'y'}]
My question is: instead of pulling the entire RatingScope with multiple dictionaries per RatingType, how can I modify the code to retrieve separate columns for each RatingType? Specifically, I would like to extract the Agency, CurrentRating, and CurrentRatingDate for each of the 21 rating types, resulting in 63 columns (3 per RatingType).
For example:
- Column 1: 'RatingType': 'MDT', 'Agency'
- Column 2: 'RatingType': 'MDT', 'CurrentRating'
- Column 3: 'RatingType': 'MDT', 'CurrentRatingDate'
- Column 4: 'RatingType': 'MDR', 'Agency'
- Column 5: 'RatingType': 'MDR', 'CurrentRating'
- Column 6: 'RatingType': 'MDR', 'CurrentRatingDate'
And so on for all 21 rating types.
I appreciate your assistance and look forward to your suggestions.