model_clo_insight.utilities module

Module for utilities used across the code

clean_numerical_output(x)

Rounds numerical output for consistency between different clients

By rounding to 6 digits there should not be an issue when running pooling using different clients.

Parameters

x – float

Returns: float

convert_rtg(df, column_name)

converts ratings for full column in Pandas dataframe to high/low format

Parameters
  • df – Pandas Dataframe

  • column_name – column we are converting ratings from

Returns

corrected dataframe column of ratings

convert_single_rtg(value, warr)

converts single rating to high/low format

Parameters
  • value – rating

  • warr – if rating is used for WARR is deal matrices, we keep it as capitalized

Returns

corrected rating

dataframe_dropna(df)

takes in dataframe and drops all NA values

Parameters

df – Pandas Dataframe

Returns

Pandas dataframe with no NA values

dataframe_to_list_of_dataclass(df, my_dataclass, header=True, dropna=True)

takes dataframe and converts it to a list of dataclasses

Parameters
  • df – Pandas dataframe

  • my_dataclass – dataclass format we want it to be in

Returns

List of dataclass

json_encoder(o)

JSON encoder class.

Notes;

Handling timestamp formatting and NaT from pandas.

Parameters

o (object) – object to serialise

Returns

encoded object

Return type

object

listSerDe(list_to_serialise, ignore_nan=True)

Non Python native objects serialiser handler.

Notes;

Numpy nan or datetime are not natively serialise by JSON (or not correctly). We do the SerDe ourselves to overcome this issue and stick wih native dictionary python as exit. Probably not the most efficient, but the faster option.

Parameters
  • list_to_serialise (Series, DataFrame) – object to SerDe

  • ignore_nan (bool) – ignore nan values

Returns

SerDe Python dictionary

Return type

dict

list_dropna(ls)

takes in list and drops all NA values

Parameters

ls – list

Returns

List with no NA values

list_of_dataclass_to_dataframe(list_of_dataclass)

takes list of dataclass and converts it to a Pandas dataframe

Parameters

dataclass (list of) –

Returns

Pandas dataframe

list_of_lists_dropna(ls)

takes in a list of lists and drops all NA values

Parameters

ls – List of lists

Returns

List of lists with no NA values

list_of_lists_to_dataclass(df, my_dataclass, header=False)

takes in list of lists and converts to dataclass

Parameters
  • df – list of lists taken in

  • my_dataclass – dataclass structure we are using

Returns

dataclass object