Skip to content
Snippets Groups Projects
Commit 9fdef4e0 authored by Colm Talbot's avatar Colm Talbot
Browse files

Merge branch 'json-pandas-series' into 'master'

Make pandas series objects serialisable

See merge request lscsoft/bilby!807
parents dc203f13 c89630f6
No related branches found
No related tags found
No related merge requests found
Pipeline #138896 failed
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment