quantlab.utils.jsonable.to_jsonable#
- quantlab.utils.jsonable.to_jsonable(value)[source]#
Return
valuerebuilt from JSON-safe types only.Conversion rules:
dictbecomes a dict withstrkeys and converted values; two keys that are equal afterstr()raiseValueError.list,tuple,np.ndarray(any rank),pd.Seriesandpd.Indexbecome lists, element by element; a 0-d array becomes its scalar;setandfrozensetbecome lists in a deterministic order.Booleans and integers (Python or numpy) become
boolandint; floats becomefloatwith NaN and infinities mapped toNone.Timestamps,
datetime64,datetimeanddatebecome ISO-8601 strings; timedeltas becomestr;NaTof either kind becomesNone;PathandDecimalbecomestr; anEnumis replaced by its converted value;str,intandNonepass through.
- Parameters:
- valueobject
The object to convert.
- Returns:
- object
A structure made only of dict, list, str, int, float, bool and None.
- Raises:
- ValueError
If two dict keys collide after
str()coercion.- TypeError
If
value(or any nested value) has an unsupported type.
Examples
>>> to_jsonable({"sharpe": np.float64("nan"), "start": pd.Timestamp("2024")}) {'sharpe': None, 'start': '2024-01-01T00:00:00'}