pandas报错处理:TypeError: Empty 'DataFrame': no numeric data to plot

Tushare返回的是pandas的DataFrame格式,可是执行如下代码时报错:TypeError: Empty 'DataFrame': no numeric data to plothtml

import tushare as ts df_all = ts.realtime_boxoffice() df_box_office = df['BoxOffice'] df_box_office.index = df['Irank'] df_box_office.plot(kind='bar')

反复输出df['BoxOffice']确认是有值的,想不通为何会报“Empty”,百度也没什么结果。git

觉得TuSahre和pandas结合时有bug准备放弃的时候看到了报错中有“numeric”,莫非是说df['BoxOffice']是字符串类型不是数值类型因此致使报错github

在pandas官方文档看到了“astype()”的用法,代码改为以下成功绘图(另外使用“pd.to_numeric()”也能够,不过从tushare又得绕回pandas一趟感受代码看起来没那么顺畅):spa

import tushare as ts df_all = ts.realtime_boxoffice() df_box_office = df['BoxOffice'].astype(float) df_box_office.index = df['Irank'] df_box_office.plot(kind='bar')

相关文章
相关标签/搜索