【问题标题】:Bokeh: ValueError: Out of range float values are not JSON compliant散景:ValueError:超出范围的浮点值不符合 JSON
【发布时间】:2016-12-13 18:03:01
【问题描述】:

我遇到了这个讨论(一年前):https://github.com/bokeh/bokeh/issues/2392

我也看到了没有任何错误的白屏..然后我尝试获取 2 列的一小部分并尝试以下操作:

由于 pandas 也只得到了一堆空数据行,所以我尝试了 dropna.. 这导致根本没有数据。因此,我只是指定了应该进入 df 的行(因此是 df = df.head(n=19) 行)

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

上面显示了笔记本中的输出,但仍然抛出:ValueError: Out of range float values are not JSON compliant

而且它也不(因此?)生成输出文件。我如何摆脱这个小子集的这个错误?它与 NaN 值有关吗?这也能解决更大数据集的“死机白屏”问题吗?

感谢 vm 观看!

如果您想查看整个错误:

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

【问题讨论】:

    标签: python bokeh


    【解决方案1】:

    我遇到了同样的错误,我调试了问题:我的绘图数据集中有 NaN 值,bokehserialize_json() 函数(在 /core/json_encoder.py 中)不允许 NaN 值(我不不知道为什么...)。在此函数的return 部分中,json.dumps() 中有allow_nan=False 参数 :(( 问题仅出现在生成输出文件时散景过程的io 部分(它调用上述serialize_json()函数)。

    因此,您必须替换数据框中的 NaN 值,例如:

    df = df.fillna('')
    

    美好的一天! :)

    【讨论】:

    • 去掉NaN后,问题就解决了。
    【解决方案2】:

    NaN 支持将在 this Pull Request 添加二进制数组序列化选项合并时得到更好的支持。这应该在 2017 年 1 月的 Bokeh 0.12.4 中可用。Bokeh 在 python JSON 编码器中不使用 allow_nan,因为这不是标准的 - naninf 不是官方 JSON 规范的一部分(一个令人震惊的监督 IMO,但我们无法控制)

    【讨论】:

      【解决方案3】:

      嗯,这并不完全是您问题的答案,它更像是我使用散景一周的经验。在我的情况下,试图从散景中制作像德克萨斯州示例这样的情节......经过很多挫折后,我注意到散景或 json 或任何遇到列表的第一个值(myList)时被绘制为 NaN它拒绝密谋传递信息

      ValueError: Out of range float values are not JSON compliant

      如果我将列表的第一个值 (myList[0]) 更改为浮动,即使它包含 NaN 到其他位置,它也可以正常工作。考虑到这一点,了解这些事情如何运作的人会提出答案。我的任务是重构您的数据,使第一个值不是 nan。

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,我意识到它正在发生,因为我的 Dataframe 的一列只填充了 NaN。

        您可以改为将其设置为另一个值,例如:

        df['column'] = np.zeros(len(df))
        

        【讨论】:

          【解决方案5】:

          我在这一行遇到了这个错误:

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

          我使用这个 repo 作为基线:https://github.com/ShuaiW/twitter-analysis/blob/master/topic_tweets.py (mine)

          而且,我用这段代码解决了这个问题(希望这对其他人有用):

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

          关键是:

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

          【讨论】:

            【解决方案6】:

            我遇到了这个问题,并通过 clean my dataset

            解决了这个问题

            检查您的数据集并更改null记录值。

            【讨论】:

              【解决方案7】:

              去掉 NAN 值后,可能会有无穷大的值, 跟踪整个数据集,它可能具有一些无限值,因为inf 以某种方式删除这些无限值,然后它应该可以工作。

              df['column'].describe()
              

              然后,如果您发现任何 inf 值,请使用

              删除这些行
              df = df[~df.isin([np.nan, np.inf, -np.inf]).any(1)]
              

              参考:solution here

              【讨论】:

                猜你喜欢
                • 2020-10-30
                • 2022-11-10
                • 2023-03-27
                • 2020-10-22
                • 2023-03-12
                • 1970-01-01
                • 2018-07-18
                • 1970-01-01
                相关资源
                最近更新 更多