ValueError:预期为dict或pandas.DataFrame

时间:2018-10-23 16:30:49

标签: python pandas bokeh

我正在尝试创建一个分类vBar,该vBar将显示为各种操作而拾取的移民总数,但是,当我将“ groupby” pandas对象传递给列数据源时,我不断收到错误消息,并且我不太确定我在做什么错。

我曾在一些地方寻找过类似的问题,但是我似乎找不到任何答案。

有人能指出我正确的方向吗?

#Imports
import pandas as pd
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.models.tools import HoverTool
from bokeh.models import Button
from bokeh.layouts import row

#Global variables
viewFilter='Operation'

#Data
df = pd.read_csv('data.csv')
grouped = df.groupby(viewFilter)['Total Migrants']
source = ColumnDataSource(grouped)
#grouped = df.groupby(viewFilter)['Total Migrants'].sum()
#source = ColumnDataSource(pd.DataFrame(grouped))
operations = source.data[viewFilter].tolist()

# Fig Creation Function 
def create_figure():
    global viewFilter
    p=figure(x_range=operations)
    p.vbar(x=viewFilter, top='Total Migrants', 
           source=source, width=0.70)

    p.title.text='Demo Chart'
    p.xaxis.axis_label = viewFilter
    p.yaxis.axis_label = 'Total Migrants'

    #Hover took
    hover = HoverTool()
    hover.tooltips=[
        ("Total Migrants Rescued", "@{Total Migrants}")]
    hover.mode='vline'
    p.add_tools(hover)

    return p

#Update Data with Ship-level aggregation
def shipUpdate():
    print("Ship Button was Pushed")

#Widgets
shipButton = Button(label='Ship Level')
shipButton.on_click(shipUpdate)

#Implement Layout
layout = row(shipButton, create_figure())

#Add Layout to Document
curdoc().add_root(layout)

3 个答案:

答案 0 :(得分:1)

好像在groupby()方法或ColumnDataSource()中传递了错误的参数值

语法:

DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs)

参数by->列表,str,dict

下面是ColumnDataSource的构造方法的代码段-

def __init__(self, *args, **kw):
        ''' If called with a single argument that is a dict or
        pandas.DataFrame, treat that implicitly as the "data" attribute.

        '''
        if len(args) == 1 and "data" not in kw:
            kw["data"] = args[0]

        # TODO (bev) invalid to pass args and "data", check and raise exception
        raw_data = kw.pop("data", {})

        if not isinstance(raw_data, dict):
            if pd and isinstance(raw_data, pd.DataFrame):
                raw_data = self._data_from_df(raw_data)
            elif pd and isinstance(raw_data, pd.core.groupby.GroupBy):
                raw_data = self._data_from_groupby(raw_data)
            else:
                raise ValueError("expected a dict or pandas.DataFrame, got %s" % raw_data)
        super(ColumnDataSource, self).__init__(**kw)
        self.data.update(raw_data)

答案 1 :(得分:1)

您的Bokeh版本过旧。在版本0.12.7中添加了对传递熊猫GroupBy对象的支持。如果您希望能够直接传递GroupBy对象来初始化CDS(例如,访问创建的所有自动摘要统计信息),则需要升级到新版本。

答案 2 :(得分:0)

看来,如果我显式传递pandas数据框对象,它将解决此错误:

source = ColumnDataSource(pd.DataFrame(grouped))