Closed3

python bokehでinfを扱えない

yuji38kwmtyuji38kwmt

原因はpandas DataFrameにinfが含まれていた。

    bokeh.plotting.save(bokeh.layouts.column([div_element] + figure_list))
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/io/saving.py", line 86, in save
    _save_helper(obj, filename, resources, title, template, theme)
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/io/saving.py", line 148, in _save_helper
    html = file_html(obj, resources, title=title, template=template, theme=theme)
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/embed/standalone.py", line 304, in file_html
    (docs_json, render_items) = standalone_docs_json_and_render_items(models_seq, suppress_callback_warning=suppress_callback_warning)
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/embed/util.py", line 294, in standalone_docs_json_and_render_items
    docs_json[docid] = doc.to_json()
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/document/document.py", line 902, in to_json
    doc_json = self.to_json_string()
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/document/document.py", line 959, in to_json_string
    return serialize_json(json, indent=indent)
  File "/home/vagrant/.cache/pypoetry/virtualenvs/annofabcli-DUhmmfBn-py3.8/lib/python3.8/site-packages/bokeh/core/json_encoder.py", line 166, in serialize_json
    return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)
  File "/home/vagrant/.pyenv/versions/3.8.6/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/home/vagrant/.pyenv/versions/3.8.6/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/vagrant/.pyenv/versions/3.8.6/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant

yuji38kwmtyuji38kwmt

今はどうなのか?

yuji38kwmtyuji38kwmt

bokeh 3.4.0 in Python 3.12.1 で確認

from bokeh.plotting import ColumnDataSource, figure, output_file, save
import pandas

output_file("sample.html")

df = pandas.DataFrame({"a": [0, 0, 1, 2], "b": [10, 0, 12, 13]})
df["b/a"] = df["b"] / df["a"]

print(df)

source = ColumnDataSource(df)
fig = figure()
fig.scatter(x="a", y="b/a", source=source)
save(fig)

エラーは発生しなかった。inf, nanは描画されなかった。期待通り。

このスクラップは2021/03/03にクローズされました