简体   繁体   English

散景-日期时间 x_range: 'ValueError, Unrecognized range input'

[英]Bokeh- datetime x_range: 'ValueError, Unrecognized range input'

I have a dataframe df2 that looks like:我有一个数据df2看起来像:

DATE       | STATUS
2018-02-01     A
2018-02-02     A
2018-02-02     B
..
2018-02-05     B

All the values are string type.所有值都是字符串类型。 I cannot seem to get the dates recognized as dates and plotted onto the x-axis of a stacked bar chart.我似乎无法将日期识别为日期并绘制到堆积条形图的 x 轴上。 Here is the most recent of my futile attempts:这是我最近一次徒劳的尝试:

import pandas as pd
from bokeh.io import curdoc
import numpy as np
from bokeh.models import ColumnDataSource, HoverTool,DatetimeTickFormatter
from bokeh.core.properties import value
from bokeh.plotting import figure

#prepare x axis
df2['DATE']=pd.to_datetime(df2['DATE'])
Months=df2["DATE"].tolist()
Months=list(set(Months))  #remove duplicates

IBs=df2["STATUS"].tolist()
IBs=list(set(IBs))

#pivot dataframe
#df2["DATE"]=df2["DATE"].str.strip() #trim values in column
df2=df2.pivot_table(index=['DATE'],columns=['STATUS'], aggfunc=len)
df2=df2.reset_index()
df2=df2.fillna(0) #replace nans with 0

#add plot
source=ColumnDataSource(data=df2)
p= figure(x_range= df2["DATE"], plot_width=700, plot_height=400, x_axis_type='datetime',
x_axis_label='Month', y_axis_label='count')
p.xaxis[0].formatter = DatetimeTickFormatter(days="%Y/%m/%d")

p.vbar_stack(IBs, x='DATE', width=0.9, source=source, legend=[value(x) for x in IBs])

curdoc().add_root(p)

thanks in advance.提前致谢。

edit: the errors are Urecognized range input: [Timestamp('2018-02-02 00:00:00').., Timestamp('2018-02-03 00:00:00')]编辑:错误是Urecognized range input: [Timestamp('2018-02-02 00:00:00').., Timestamp('2018-02-03 00:00:00')]

I had the same issue and just figured out what was the problem (at least in my case).我遇到了同样的问题,只是想出了问题所在(至少在我的情况下)。

Basically, whatever object you pass into x_range, it must be iterable, containing string type elements only.基本上,无论您传递给 x_range 的任何对象,它都必须是可迭代的,仅包含字符串类型的元素。

Therefore, convert df2["DATE"] 's elements' type to str before you pass into x_range.因此,在传入 x_range 之前,将df2["DATE"]的元素类型转换为 str 。

p = figure(x_range=df2["DATE"], ...)  # df2["DATE"] = list(map(str, df['score']))

partial fix: removing the paramerter x_range from figure() , but my bars disappeared部分修复:从figure()删除参数x_range ,但我的条形消失了

i suspect my bars disappeared because of this: categorical y-axis and datetime x-axis with Bokeh vbar plot我怀疑我的条形图因此消失了: 带有散景 vbar 图的分类 y 轴和日期时间 x 轴

I suspect我猜测

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM