traittypes
traittypes copied to clipboard
ValueError: Can't clean for JSON
I'm using DataFrame from Traittypes and I'm getting the error ValueError: Can't clean for JSON
. I'm showing in the output that I have a normal dataframe df1
... not sure what could be causing the issue?
I have the same issue while ioiterate through my dataframes!!!! :(
I don't know anything specific about your cases, but in general ValueError: Can't clean for JSON
means some widget trait was not correctly serialized. To be able to offer any help at all, you would need to share a minimal example we can run to reproduce the error.
I have the same issue, and everything worked before I added an Array
trait using this library ...
If someone could please share some code to reproduce this error I might be able to help.
@vidartf Here's my minimum contrived example that produces this bug:
from IPython import get_ipython
import datetime
import pandas as pd
def dataframe_formatter(df):
return { 'application/the21st': { 'rows': [{'datetime': datetime.time(10, 0) }]} }
get_ipython().display_formatter.mimebundle_formatter.for_type(pd.DataFrame, dataframe_formatter)
Displaying any dataframe in jupyter then produces an error with stack trace:
error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-ed84ad4d2d82> in <module>
---> 1 df
/opt/venv/lib/python3.7/site-packages/IPython/core/displayhook.py in __call__(self, result)
264 self.fill_exec_result(result)
265 if format_dict:
--> 266 self.write_format_data(format_dict, md_dict)
267 self.log_output(format_dict)
268 self.finish_displayhook()
/opt/venv/lib/python3.7/site-packages/ipykernel/displayhook.py in write_format_data(self, format_dict, md_dict)
69
70 def write_format_data(self, format_dict, md_dict=None):
---> 71 self.msg['content']['data'] = json_clean(encode_images(format_dict))
72 self.msg['content']['metadata'] = md_dict
73
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in json_clean(obj)
189 out = {}
190 for k,v in iteritems(obj):
--> 191 out[unicode_type(k)] = json_clean(v)
192 return out
193 if isinstance(obj, datetime):
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in json_clean(obj)
189 out = {}
190 for k,v in iteritems(obj):
--> 191 out[unicode_type(k)] = json_clean(v)
192 return out
193 if isinstance(obj, datetime):
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in json_clean(obj)
175
176 if isinstance(obj, list):
--> 177 return [json_clean(x) for x in obj]
178
179 if isinstance(obj, dict):
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in <listcomp>(.0)
175
176 if isinstance(obj, list):
--> 177 return [json_clean(x) for x in obj]
178
179 if isinstance(obj, dict):
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in json_clean(obj)
189 out = {}
190 for k,v in iteritems(obj):
--> 191 out[unicode_type(k)] = json_clean(v)
192 return out
193 if isinstance(obj, datetime):
/opt/venv/lib/python3.7/site-packages/ipykernel/jsonutil.py in json_clean(obj)
195
196 # we don't understand it, it's probably an unserializable object
--> 197 raise ValueError("Can't clean for JSON: %r" % obj)
ValueError: Can't clean for JSON: datetime.time(10, 0)
@the21st But that code doesn't use the traittypes
package at all. Could you explain a little more what code in this repo that example is relevant for?
@vidartf you're right. I think it might an issue in ipykernel
that's the root cause. Just wanted to illustrate how it might happen.
Edit: I opened an issue there https://github.com/ipython/ipykernel/issues/546
Well, as there isn't a standard JSON primitive for dates, you would be responsible for serializing the data/datetime/time object yourself.