Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Can't clean for JSON #35

Open
nscozzaro opened this issue Apr 26, 2019 · 8 comments
Open

ValueError: Can't clean for JSON #35

nscozzaro opened this issue Apr 26, 2019 · 8 comments

Comments

@nscozzaro
Copy link

nscozzaro commented Apr 26, 2019

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?
image

@Abonia1
Copy link

Abonia1 commented Mar 19, 2020

I have the same issue while ioiterate through my dataframes!!!! :(

@vidartf
Copy link
Member

vidartf commented Mar 20, 2020

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.

@wlievens
Copy link

I have the same issue, and everything worked before I added an Array trait using this library ...

@vidartf
Copy link
Member

vidartf commented Apr 3, 2020

If someone could please share some code to reproduce this error I might be able to help.

@the21st
Copy link

the21st commented Sep 5, 2020

@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)

@vidartf
Copy link
Member

vidartf commented Sep 7, 2020

@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?

@the21st
Copy link

the21st commented Sep 8, 2020

@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 ipython/ipykernel#546

@vidartf
Copy link
Member

vidartf commented Sep 8, 2020

Well, as there isn't a standard JSON primitive for dates, you would be responsible for serializing the data/datetime/time object yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants