Bokeh: ValueError: Out of range float values are not JSON compliant

Related searches

I came across this discussion (from a year ago): https://github.com/bokeh/bokeh/issues/2392

I also saw the white screen without any errors..and then i tried to take a small subset of 2 columns and tried the below:

Since pandas just gets a bunch of rows with empty data in there as well, I tried dropna.. this resulted in there being no data at all. So instead I just specified the rows that should go into the df (hence the df = df.head(n=19) line)

import pandas as pd
from bokeh.plotting import figure, output_file, show

df = pd.read_excel(path,sheetname,parse_cols="A:B")
df = df.head(n=19)
print(df)
rtngs = ['iAAA','iAA+','iAA','iAA-','iA+','iA','iA-','iBBB+','iBBB','iBBB-','iBB+','iBB','iBB-','iB+','iB','iB-','NR','iCCC+']
x= df['Score']
output_file("line.html")

p = figure(plot_width=400, plot_height=400, x_range=(0,100),y_range=rtngs)

# add a circle renderer with a size, color, and alpha
p.circle(df['Score'], df['Rating'], size=20, color="navy", alpha=0.5)

# show the results
#output_notebook()
show(p)

df:

   Rating  Score
0    iAAA   64.0
1    iAA+   33.0
2     iAA    7.0
3    iAA-   28.0
4     iA+   36.0
5      iA   62.0
6     iA-   99.0
7   iBBB+   10.0
8    iBBB   93.0
9   iBBB-   91.0
10   iBB+   79.0
11    iBB   19.0
12   iBB-   95.0
13    iB+   26.0
14     iB    9.0
15    iB-   26.0
16     NR   49.0
17  iCCC+   51.0
18   iAAA   18.0

The above is showing me an output within the notebook, but still throws : ValueError: Out of range float values are not JSON compliant

And also it doesn't (hence?) produce the output file as well. How do I get rid of this error for this small subset? Is it related to NaN values? Would that also solve the 'white screen of death' issue for the larger dataset?

Thanks vm for taking a look!

In case you would like to see the entire error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-4fa6b88aa415> in <module>()
     16 # show the results
     17 #output_notebook()
---> 18 show(p)

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\io.py in show(obj, browser, new)
    300     if obj not in _state.document.roots:
    301         _state.document.add_root(obj)
--> 302     return _show_with_state(obj, _state, browser, new)
    303 
    304 

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\io.py in _show_with_state(obj, state, browser, new)
    310 
    311     if state.notebook:
--> 312         comms_handle = _show_notebook_with_state(obj, state)
    313         shown = True
    314 

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\io.py in _show_notebook_with_state(obj, state)
    334         comms_target = make_id()
    335         publish_display_data({'text/html': notebook_div(obj, comms_target)})
--> 336         handle = _CommsHandle(get_comms(comms_target), state.document, state.document.to_json())
    337         state.last_comms_handle = handle
    338         return handle

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\document.py in to_json(self)
    792         # this is a total hack to go via a string, needed because
    793         # our BokehJSONEncoder goes straight to a string.
--> 794         doc_json = self.to_json_string()
    795         return loads(doc_json)
    796 

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\document.py in to_json_string(self, indent)
    785         }
    786 
--> 787         return serialize_json(json, indent=indent)
    788 
    789     def to_json(self):

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\core\json_encoder.py in serialize_json(obj, encoder, indent, **kwargs)
     97         indent = 2
     98 
---> 99     return json.dumps(obj, cls=encoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    235         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    236         separators=separators, default=default, sort_keys=sort_keys,
--> 237         **kw).encode(obj)
    238 
    239 

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\json\encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

C:\Users\x\AppData\Local\Continuum\Anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

ValueError: Out of range float values are not JSON compliant

I had the same error and I debugged the problem: I had NaN values in my plotted dataset and bokeh's serialize_json() function (in /core/json_encoder.py) does not allow NaN values (I don't know why...). In the return part of this function there is the allow_nan=False argument in json.dumps() :(( The problem occurs only at the io part of bokeh process when the output file is generated (it calls the above serialize_json() function).

So you have to replace NaN values in your dataframe, eg.:

df = df.fillna('')

Nice day! :)

Bokeh: ValueError: Out of range float values are not JSON compliant , I had the same error and I debugged the problem: I had NaN values in my plotted dataset and bokeh 's serialize_json() function (in /core/json_encoder.py ) does� @bigreddot data comes from check_matrix. On it's columns, there are 'ratio' and 'total' values for different module IDs. 'Ratio' columns range from 0 to 1 with Nan's in them, and the 'total' columns consist of integers having 0 sometimes.

NaN support will be better supported when this Pull Request to add a binary array serialization option is merged. This should be available in Bokeh 0.12.4 in January 2017. Bokeh does not use allow_nan in the python JSON encoder, because that is not standard — nan and inf are not part of the official JSON specification (an egregious oversight IMO, but out of our control)

ValueError: Out of range float values are not JSON compliant, I put this on stackoverflow, but didn't get much love so putting it here:� Summary. Issue on versions 0.12.5 when plotting data with patches. Error code is ValueError: Out of range float values are not JSON compliant. Expected behavior

Well it isn't exactly an answer to your question it's more like my experience working with bokeh for a week. In my case trying to make a plot like the Texas example from bokeh..... After a lot of frustration i noticed that bokeh or json or whatever when encounters the first value of the list (myList) to be plotted to be a NaN it refuses to plot giving the message

ValueError: Out of range float values are not JSON compliant

if i change the first value of the list (myList[0]) to float it works fine even if it contains NaN's to other positions. Taking this in account someone who understands how these things work will propose an answer. Mine is to restruct your data so that the first value isn't a nan.

"ValueError: Out of range float values are not JSON compliant", Ask questionsValueError: Out of range float values are not JSON compliant. Hello , i'm getting and error with show(p, notebook_handle=True) , with show(p) it's� I suspect that the value of logs["val_loss"] is out of range for the failed trail. Sometimes if learning rate is too large, the loss value could be 'nan'. 👍 1

I had this error in this line:

save(plot_lda, 'tsne_lda_viz_{}_{}_{}_{}_{}_{}_{}.html'.format(
    num_qualified_tweet, n_topics, threshold, n_iter, num_example, n_top_words, end_date))

I worked use this repo as baseline: https://github.com/ShuaiW/twitter-analysis/blob/master/topic_tweets.py (mine)

And, i solved this with this code (hope this will useful for others):

  for i in range(X_topics.shape[1]):
    topic_coord[i, 0] = 0 if np.isnan(topic_coord[i, 0]) else topic_coord[i, 0]
    topic_coord[i, 1] = 0 if np.isnan(topic_coord[i, 1]) else topic_coord[i, 1]
    plot_lda.text(topic_coord[i, 0], topic_coord[i, 1], [topic_summaries[i]])

The key is:

var = 0 if np.isnan(number) else number

ValueError: Out of range float values are not JSON compliant` DuCorey mentioned this issue May 1, 2017 ValueError: Out of range float values are not JSON compliant version 0.12.5 #6222

After removing the NAN values, there might be infinite value, Trace the whole dataset it might have some infinite values as inf remove those infinite values some how, then it should work.

df['column'].describe()

then if you find any inf value remove those rows with

df = df[~df.isin([np.nan, np.inf, -np.inf]).any(1)]

reference: solution here

I put this on stackoverflow, but didn’t get much love so putting it here: http://stackoverflow.com/questions/38821132/bokeh-valueerror-out-of-range-float-values-are

Out of range float values are not JSON compliant with numpy arrays #7523

"If allow_nan is False (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity)." So, this is a feature, not a bug.

`ValueError: Out of range float values are not JSON compliant` when the 1st column of the df is all nans #5584 Closed rs2 opened this issue Dec 19, 2016 · 1 comment