Skip to content
Snippets Groups Projects

Make pandas series objects serialisable

Merged Colm Talbot requested to merge json-pandas-series into master
1 file
+ 4
0
Compare changes
  • Side-by-side
  • Inline
+ 4
0
@@ -1021,6 +1021,8 @@ class BilbyJsonEncoder(json.JSONEncoder):
return {'__complex__': True, 'real': obj.real, 'imag': obj.imag}
if isinstance(obj, pd.DataFrame):
return {'__dataframe__': True, 'content': obj.to_dict(orient='list')}
if isinstance(obj, pd.Series):
return {'__series__': True, 'content': obj.to_dict()}
if inspect.isfunction(obj):
return {"__function__": True, "__module__": obj.__module__, "__name__": obj.__name__}
if inspect.isclass(obj):
@@ -1098,6 +1100,8 @@ def decode_bilby_json(dct):
return complex(dct["real"], dct["imag"])
if dct.get("__dataframe__", False):
return pd.DataFrame(dct['content'])
if dct.get("__series__", False):
return pd.Series(dct['content'])
if dct.get("__function__", False) or dct.get("__class__", False):
default = ".".join([dct["__module__"], dct["__name__"]])
return getattr(import_module(dct["__module__"]), dct["__name__"], default)
Loading