Bokeh BoxPlot> KeyError:“标签[SomeCategory]不在[index]中”

时间:2018-12-31 13:43:09

标签: python-3.x

我正在尝试使用Bokeh创建BoxPlot。当我到达需要识别异常值的部分时,如果给定类别没有异常值,则失败。

如果删除“问题”类别,则BoxPlot将正确执行。只有当我尝试使用没有异常值的类别创建此BoxPlot时,它才会失败。

是否有任何有关如何补救此问题的说明?

失败发生在注释部分“准备用于绘制[...]的异常数据”

import numpy as np
import pandas as pd
import datetime
import math

from bokeh.plotting import figure, show, output_file
from bokeh.models import NumeralTickFormatter

# Create time stamps to allow for figure to display span in title
today  = datetime.date.today()
delta1 = datetime.timedelta(days=7)
delta2 = datetime.timedelta(days=1)
start  = str(today - delta1)
end    = str(today - delta2)

#Identify location of prices
itemloc = 'Everywhere'

df = pd.read_excel(r'C:\Users\me\prices.xlsx')

# Create a list from the dataframe that identifies distinct categories for the separate box plots
cats = df['subcategory_desc'].unique().tolist()

# Find the quartiles and IQR for each category
groups = df.groupby('subcategory_desc', sort=False)
q1 = groups.quantile(q=0.25)
q2 = groups.quantile(q=0.5)
q3 = groups.quantile(q=0.75)
iqr = q3 - q1
upper = q3 + 1.5*iqr
lower = q1 - 1.5*iqr

# Find the outliers for each category
def outliers(group):
    cat = group.name
    return group[(group.price > upper.loc[cat][0]) | (group.price < lower.loc[cat][0])]['price']
out = groups.apply(outliers).dropna()

# Prepare outlier data for plotting, we need coordinates for every outlier.
outx = []
outy = []
for cat in cats:
    # only add outliers if they exist
    if not out.loc[cat].empty:
        for value in out[cat]:
            outx.append(cat)
            outy.append(value)

我希望没有异常值的类别的“箱须”部分只会出现没有异常点的情况。

1 个答案:

答案 0 :(得分:0)

您是否尝试过官方文档https://docs.bokeh.org/en/latest/docs/gallery/boxplot.html中的代码?

# prepare outlier data for plotting, we need coordinates for every outlier.
if not out.empty:
    outx = []
    outy = []
    for keys in out.index:
        outx.append(keys[0])
        outy.append(out.loc[keys[0]].loc[keys[1]])