Package: pyratings
pyratings — Working with credit ratings, professionally and efficientlyDocumentation:https://hsbc.github.io/pyratings/Source Code:https://github.com/hsbc/pyratings/What is it all about?Do you work in the investment industry?
Do you work with fixed income instruments, such as bonds and credit securities?
Then you are probably concerned with credit ratings, too?This Python library will be useful for portfolio managers, credit analysts, as well
as anybody who is working with credit ratings.
It provides functions, which will be helpful in order to work with ratings in
a professional and efficient way.Table of contentsWhat haspyratingsto offer and how might it help you?How to installpyratings?Getting started withpyratingsCleaning ratingsConsolidating ratingsTranslating ratingsSupportContributingAcknowlegdementsLicenseWhat haspyratingsto offer and how might it help you?Do you need to compute the average credit rating of an investment portfolio?
Do you need to compute the worst rating for individual securities', given these
securities have ratings attached from more than one rating agency?
Do you need to compute the Weighted Average Rating Factor (WARF)?If yes, you might want to take a look atpyratingsand its offerings in the credit
ratings space.pyratingsoffers the following capabilities:Cleaning ratings for further processing, e.g. stripping off of rating watches.Transform long- and short-term ratings into rating scores and vice versa.Compute the best/second best/worst ratings on a security level basis within a
portfolio context.Compute average ratings/rating scores on a portfolio level.Compute Weighted Average Rating Factor (WARF) on a portfolio level.Compute WARF buffer, i.e. distance from current WARF to the next maxWARF.pyratingssupportslong-term ratingsas well asshort-term ratings.
Currently, the following rating agencies will be supported:Moody's (long-term / short-term)Standard & Poors (long-term / short-term)Fitch (long-term / short-term)DBRS Morningstar (long-term / short-term)Bloomberg (long-term)How to installpyratings?pyratingsis listed onpypi.
Make sure to have Python 3.9+ installed on your machine.Usingpip:pipinstallpyratingsUsingpdm:pdmaddpyratingsUsingpoetry:poetryaddpyratingsGetting started withpyratingsHere are some very basic code snippets to get you up and running.Cleaning ratingsSometimes, a rating has a credit watch or an outlook attached to it.
In order to work with this rating, it is usually necessary to get rid of it.>>>importpyratingsasrtg

>>>rtg.get_pure_ratings("AA- *+")"AA-"Cleaning a pandas datraframe, which comprises several securities with ratings from S&P
and Fitch:>>>importnumpyasnp
>>>importpandasaspd

>>>importpyratingsasrtg


>>>rtg_df=pd.DataFrame(...data={..."rtg_SP":[..."BB+ *-",
..."BBB *+",
...np.nan,
..."AA- (Developing)",
...np.nan,
..."CCC+ (CwPositive)",
..."BB+u",
...],
..."rtg_Fitch":[..."BB+ *-",
..."BBB *+",
...np.nan,
..."AA- (Developing)",
...np.nan,
..."CCC+ (CwPositive)",
..."BB+u",
...],
...},
...)>>>rtg_dfrtg_SPrtg_Fitch0BB+*-BB+*-1BBB*+BBB*+2NaNNaN3AA-(Developing)AA-(Developing)4NaNNaN5CCC+(CwPositive)CCC+(CwPositive)6BB+uBB+u# Get rid of all the noise.>>>rtg.get_pure_ratings(rtg_df)rtg_SP_cleanrtg_Fitch_clean0BB+BB+1BBBBBB2NaNNaN3AA-AA-4NaNNaN5CCC+CCC+6BB+BB+Consolidating ratingsIt is quite common that an individual credit security has been rated by several
credit agencies.
In this case, you may want to compute the best or worst rating.>>>importpandasaspd

>>>importpyratingsasrtg

>>>ratings_df=pd.DataFrame(...data=(...{..."rating_S&P":['AAA','AA-','AA+','BB-','C'],
..."rating_Moody":['Aa1','Aa3','Aa2','Ba3','Ca'],
..."rating_Fitch":['AA-','AA-','AA-','B+','C'],
...}...)...)>>>ratings_dfrating_S&Prating_Moodyrating_Fitch0AAAAa1AA-1AA-Aa3AA-2AA+Aa2AA-3BB-Ba3B+4CCaC# Return a pd.Series with the worst ratings.>>>rtg.get_worst_ratings(...ratings_df,rating_provider_input=["S&P","Moody","Fitch"]...)0AA-1AA-2AA-3B+4C
Name:worst_rtg,dtype:objectTranslating ratingsTo work with ratings, it's sometimes necessary to translate human-readable ratings
into numerical rating scores and vice versa.pyratingsoffers a number of functions on that front.Thedocumentationshows
in detail howpyratingstranslates human-readable ratings into numerical rating
scores.Here's an example how to translate a pandas series from ratings to scores and vice
versa.>>>importpandasaspd

>>>importpyratingsasrtg


>>>ratings_series=pd.Series(...data=["Baa1","C","NR","WD","D","B1","SD"],name='Moody'...)>>>scores_series=rtg.get_scores_from_ratings(...ratings=ratings_series,rating_provider="Moody's",tenor="long-term"...)>>>scores_series08.0121.02NaN3NaN422.0514.0622.0
Name:rtg_score_Moody,dtype:float64# Translate these rating scores back, but this time use DBRS' rating scale>>>rtg.get_ratings_from_scores(...rating_scores=scores_series,rating_provider="DBRS",tenor="long-term"...)0BBBH1C2NaN3NaN4D5BH6D
Name:rtg_DBRS,dtype:objectSupportIf you need help or have any questions, the first step should be to take a look at thedocs.
If you can't find an answer, please open an issue onGitHub, or send an email toopensource@hsbc.de.
The subject line should contain '#pyratings'ContributingWe very much welcome contributions!
Before you begin, please read ourcontributing guideliens.AcknowledgementsThanks toAndreas Vester, project creator and lead developerDeepak Parashar, for their help with
testing the code and working on all kinds of PRs.Sander Cohen, for their help to improve the
documentation as well as the intense discussion on how to best implement
short-term ratings into the project.Marco Erling, for actively participating in
discussions on how to best incorporate short-term ratings.Thomas Steenbergen, for theircode contributionregarding the
improvement of metadata in setup.py.Ekow Folson, for their help of setting up this
repository and make it possible to become the very first open source contribution
fromHSBC.LicenseThis project is licensed under the Apache 2.0 License – please see theLICENSEfile.