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: Out of range float values are not JSON compliant #5439

Closed
Evgeny7777 opened this issue Nov 3, 2016 · 17 comments
Closed

ValueError: Out of range float values are not JSON compliant #5439

Evgeny7777 opened this issue Nov 3, 2016 · 17 comments

Comments

@Evgeny7777
Copy link

Hello, i'm getting and error with show(p, notebook_handle=True), with show(p) it's ok, using 0.12.3

May provide more details if necessary, error is below


-----------------------------------------------------------------------
ValueError                            Traceback (most recent call last)
<ipython-input-180-18380363c66d> in <module>()
----> 1 show(p, notebook_handle=True)
      2 # show(p)

/home/vagrant/anaconda3/lib/python3.5/site-packages/bokeh/io.py in show(obj, browser, new, notebook_handle)
    312     if obj not in _state.document.roots:
    313         _state.document.add_root(obj)
--> 314     return _show_with_state(obj, _state, browser, new, notebook_handle=notebook_handle)
    315 
    316 

/home/vagrant/anaconda3/lib/python3.5/site-packages/bokeh/io.py in _show_with_state(obj, state, browser, new, notebook_handle)
    322 
    323     if state.notebook:
--> 324         comms_handle = _show_notebook_with_state(obj, state, notebook_handle)
    325         shown = True
    326 

/home/vagrant/anaconda3/lib/python3.5/site-packages/bokeh/io.py in _show_notebook_with_state(obj, state, notebook_handle)
    348         if comms_target:
    349             handle = _CommsHandle(get_comms(comms_target), state.document,
--> 350                                   state.document.to_json())
    351             state.last_comms_handle = handle
    352             return handle

/home/vagrant/anaconda3/lib/python3.5/site-packages/bokeh/document.py in to_json(self)
    825         # this is a total hack to go via a string, needed because
    826         # our BokehJSONEncoder goes straight to a string.
--> 827         doc_json = self.to_json_string()
    828         return loads(doc_json)
    829 

/home/vagrant/anaconda3/lib/python3.5/site-packages/bokeh/document.py in to_json_string(self, indent)
    818         }
    819 
--> 820         return serialize_json(json, indent=indent)
    821 
    822     def to_json(self):

/home/vagrant/anaconda3/lib/python3.5/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)

/home/vagrant/anaconda3/lib/python3.5/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 

/home/vagrant/anaconda3/lib/python3.5/json/encoder.py in encode(self, o)
    196         # exceptions aren't as detailed.  The list call should be roughly
    197         # equivalent to the PySequence_Fast that ''.join() would do.
--> 198         chunks = self.iterencode(o, _one_shot=True)
    199         if not isinstance(chunks, (list, tuple)):
    200             chunks = list(chunks)

/home/vagrant/anaconda3/lib/python3.5/json/encoder.py in iterencode(self, o, _one_shot)
    254                 self.key_separator, self.item_separator, self.sort_keys,
    255                 self.skipkeys, _one_shot)
--> 256         return _iterencode(o, 0)
    257 
    258 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

ValueError: Out of range float values are not JSON compliant
@bryevdv
Copy link
Member

bryevdv commented Nov 3, 2016

@Evgeny7777 there is no chance to be able to help diagnose a problem like this without much more information (OS, 32/64 bit, locale settings, etc.), as well as a complete code sample to reproduce it.

@mattpap
Copy link
Contributor

mattpap commented Nov 3, 2016

Issue #2288 seems related.

@Evgeny7777
Copy link
Author

Most probably it was NaN in data indeed, worked after clean up. Thanks

@mattpap
Copy link
Contributor

mattpap commented Nov 4, 2016

@Evgeny7777, we do actually serialize NaNs, so there may be a bug after all.

@bryevdv
Copy link
Member

bryevdv commented Nov 4, 2016

Agreed, @Evgeny7777 if you can share a minimal code sample that reproduces this it would still be helpful.

@Evgeny7777
Copy link
Author

Bryan, will try to reproduce next week. It definitely was happening with
handler=True parameter, without it was working fine - switched several times

пятница, 4 ноября 2016 г. пользователь Bryan Van de Ven написал:

Agreed, @Evgeny7777 https://github.com/Evgeny7777 if you can share a
minimal code sample that reproduces this it would still be helpful.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#5439 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADU-gyU9tBToj7a7wdUwgniZS0BFjIrcks5q63VdgaJpZM4KoMaE
.

sent from iPhone

@bryevdv
Copy link
Member

bryevdv commented Dec 21, 2016

This should be resolved entirely by #5544

@Mika15
Copy link

Mika15 commented Apr 29, 2017

SOS: Guys, I started to learn bokeh and already have a problem. Does anybody know why? Help me please

I want just one simple scatter to be drawn, have errors below:

`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
8 p=Scatter(df, x="X", y="Y", title="Temperature Obs", xlabel="Day of obs", ylabel="Temp")
9 output_file("Scatter_charts.html")
---> 10 show(p)

/anaconda/lib/python3.6/site-packages/bokeh/io.py in show(obj, browser, new, notebook_handle)
298 if obj not in _state.document.roots:
299 _state.document.add_root(obj)
--> 300 return _show_with_state(obj, _state, browser, new, notebook_handle=notebook_handle)
301
302

/anaconda/lib/python3.6/site-packages/bokeh/io.py in _show_with_state(obj, state, browser, new, notebook_handle)
316
317 if state.file or not shown:
--> 318 _show_file_with_state(obj, state, new, controller)
319
320 return comms_handle

/anaconda/lib/python3.6/site-packages/bokeh/io.py in _show_file_with_state(obj, state, new, controller)
321
322 def _show_file_with_state(obj, state, new, controller):
--> 323 filename = save(obj, state=state)
324 controller.open("file://" + filename, new=_new_param[new])
325

/anaconda/lib/python3.6/site-packages/bokeh/io.py in save(obj, filename, resources, title, state, validate)
380
381 filename, resources, title = _get_save_args(state, filename, resources, title)
--> 382 _save_helper(obj, filename, resources, title, validate)
383 return os.path.abspath(filename)
384

/anaconda/lib/python3.6/site-packages/bokeh/io.py in _save_helper(obj, filename, resources, title, validate)
445 doc.validate()
446
--> 447 html = standalone_html_page_for_models(obj, resources, title)
448
449 with io.open(filename, "w", encoding="utf-8") as f:

/anaconda/lib/python3.6/site-packages/bokeh/embed.py in standalone_html_page_for_models(models, resources, title)
598
599 '''
--> 600 return file_html(models, resources, title)
601
602 def server_html_page_for_models(session_id, model_ids, resources, title, websocket_url, template=FILE):

/anaconda/lib/python3.6/site-packages/bokeh/embed.py in file_html(models, resources, title, template, template_variables)
305
306 with _ModelInDocument(models):
--> 307 (docs_json, render_items) = _standalone_docs_json_and_render_items(models)
308 title = _title_from_models(models, title)
309 bundle = _bundle_for_objs_and_resources(models, resources)

/anaconda/lib/python3.6/site-packages/bokeh/embed.py in _standalone_docs_json_and_render_items(models)
570 docs_json = {}
571 for k, v in docs_by_id.items():
--> 572 docs_json[k] = v.to_json()
573
574 return (docs_json, render_items)

/anaconda/lib/python3.6/site-packages/bokeh/document.py in to_json(self)
949 # this is a total hack to go via a string, needed because
950 # our BokehJSONEncoder goes straight to a string.
--> 951 doc_json = self.to_json_string()
952 return loads(doc_json)
953

/anaconda/lib/python3.6/site-packages/bokeh/document.py in to_json_string(self, indent)
937 }
938
--> 939 return serialize_json(json, indent=indent)
940
941 def to_json(self):

/anaconda/lib/python3.6/site-packages/bokeh/core/json_encoder.py in serialize_json(obj, pretty, indent, **kwargs)
224 indent = 2
225
--> 226 return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)

/anaconda/lib/python3.6/json/init.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240

/anaconda/lib/python3.6/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)

/anaconda/lib/python3.6/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`

@jxramos
Copy link

jxramos commented Aug 17, 2017

Just wanted to point out this could be triggered if trying to do
figure( ... x_range = [ float('-inf'), float('inf') ] ... )

@bryevdv
Copy link
Member

bryevdv commented Aug 18, 2017

@jxramos if you need to send nan and inf values you must put your data into numpy arrays. For reasons that are 100% outside of our control, that is the only reliable efficient way to send nan and inf.

@KeesKoot48
Copy link

I still get the error from the server in bokeh 1.3.4:
error handling message Message 'PULL-DOC-REQ' (revision

  1. content: {}: ValueError('Out of range float values are not JSON compliant',)

I am using python 3.6.9. I define nan via numpy. In my plot I dont want to show the zero values and therefore convert them to nan. I see that I dont get an error (and everything works fine) if I construct the columndatasource without nan values and later do an plot update and put nan values in the columndatasource. I do get an error when I try to construct a columndatasource with nan values. (If really needed I can make up a piece of code).

@bryevdv
Copy link
Member

bryevdv commented Sep 26, 2019

@davilex404 we would definitely need a complete minimal reproducer to run in order to investigate. Just to clarify though:

I define nan via numpy.

It's not sufficient to merely define a single nan value using np.nan, if that is what you mean. Nav values can only be faithfully transmitted to BokehJS inside NumPy array (and specifically, floating point arrays) An immediate corollary is also that nan values can therefore only be part of CDS columns.

@KeesKoot48
Copy link

KeesKoot48 commented Dec 14, 2019

@bryevdv hereby a minimal reproducer:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, LinearAxis, Range1d

// I did not want to plot zero values, and therefore made them np.nan.

d = {'x': [1, 2, 3], 'y': [6, 0, 4]}
df = pd.DataFrame(data=d)

// comment this line out and: "ValueError: Out of range float values are not JSON compliant" is not there anymore.
df.loc[[1] ,'y']=np.nan

cds = ColumnDataSource(df)

p = figure(title='example')
p.scatter(source = cds, x = 'x', y = 'y', size = 10)

p.extra_y_ranges = {'y_axis2' : Range1d(cds.data['y'].min(), cds.data['y'].max())}
p.add_layout(LinearAxis(y_range_name='y_axis2'), 'right')

show(p)

@KeesKoot48
Copy link

The value error is given after calling show(p), but the problem is created by cds.data['y'].min() and cds.data['y'].max() in:
p.extra_y_ranges = {'y_axis2' : Range1d(cds.data['y'].min(), cds.data['y'].max())}

The problem is that with np.nan in the set of y values the min() and max() functions return nan: cds.data['y'].min() returns nan, if a nan is in the set. (Same applies for max()).

If there is a non-nan value in the set, I want that the min() function returns that value: this is what the function numpy.nanmin does.

@KeesKoot48
Copy link

By the way: when taking out the lines to construct the right-hand y-axis I can plot the y-values with nan values in the y dataset (the nan values are not depicted of course).

@bryevdv
Copy link
Member

bryevdv commented Dec 14, 2019

@KeesKoot48 I am not sure what you expect or are asking for. Bokeh can send NaN values in NumPy arrays that are columns in a CDS (because those are subject to a non-JSON encoding) but not elsewhere. Which is not even especially relevant the code you show: range start/end can't be NaN because there is nothing at all sensible that can be done with NaN values there. Range start/end are used to determine where and how to draw the axes. They must be actual numbers, end ensuring that, when they are set explicitly by the user, as above, is up to the user.

@bharath-kumarn
Copy link

return cls(

File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant

@bokeh bokeh locked and limited conversation to collaborators Aug 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants