无法在 Sankey HoloViews 中使用灵活类型错误执行 reduce

问题描述

我有这个代码,但我不断收到“无法减少灵活类型错误

import holoviews as hv
import pandas as pd
from holoviews import opts,dim
hv.extension('bokeh')
renderer = hv.renderer('bokeh')
    

file input path
csv_path = r'C:\Users\jose\Downloads\enron-v1.csv'
df_csv = pd.read_csv(csv_path,index_col=0)

#filtering a new dataframe
df_filter = df_csv[["fromJobtitle","toJobtitle"]]
df_final = df_filter.groupby(df_filter.columns.tolist(),as_index=False).size()

#creating a co-occurence dataframe to show relationships
df_final['combination'] = df_final.apply(lambda row: tuple(sorted([row['fromJobtitle'],row['toJobtitle']])),axis=1)
df_final = df_final.groupby('combination').sum().reset_index()
df_final['from'] = df_final.apply(lambda row: row['combination'][0],axis=1)
df_final['to'] = df_final.apply(lambda row: row['combination'][1],axis=1)
df_final = df_final.drop('combination',axis=1)
 
#turning final dataframe to a list
df_sankey = df_final.to_numpy().tolist()


sankey = hv.Sankey([df_sankey])
sankey.opts(width=600,height=400)
renderer.save(sankey,'h2')

VScode 中给出的错误信息:

File "c:/Users/js/Documents/DBL/sankey.py",line 23,in <module>
    sankey = hv.Sankey([df_sankey])
  File "C:\Users\js\AppData\Local\Programs\Python\python38\lib\site-packages\holoviews\element\sankey.py",line 349,in __init__
    nodes,edgepaths,graph = _layout_sankey.instance().layout(self)
  File "C:\Users\js\AppData\Local\Programs\Python\python38\lib\site-packages\holoviews\element\sankey.py",line 49,in layout
    self.computeNodeValues(graph)
  File "C:\Users\js\AppData\Local\Programs\Python\python38\lib\site-packages\holoviews\element\sankey.py",line 156,in computeNodeValues
    source_val = np.sum([l['value'] for l in node['sourceLinks']])
  File "<__array_function__ internals>",line 5,in sum
  File "C:\Users\jose\AppData\Local\Programs\Python\python38\lib\site-packages\numpy\core\fromnumeric.py",line 2247,in sum
    return _wrapreduction(a,np.add,'sum',axis,dtype,out,keepdims=keepdims,File "C:\Users\js\AppData\Local\Programs\Python\python38\lib\site-packages\numpy\core\fromnumeric.py",line 87,in _wrapreduction
    return ufunc.reduce(obj,**passkwargs)
TypeError: cannot perform reduce with flexible type

我尝试将实际列表 (df_sankey) 复制粘贴到 hv.sankey([]) 中,并且可以正常工作,但是当我指定输入 (df_sankeys) 时,出现此错误。关于 Holoviews 1.14 的 Sankeys 文档说可以使用 Pandas 数据框或列表作为输入,所以请找出这里出了什么问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)