python - 能够绘制为列表,无法绘制为 Pandas 系列

标签 python python-2.7 matplotlib pandas

以下数据可以使用 matplotlib 绘制,但不能在转换为 Pandas 系列后绘制。如何使用 pandas 绘制它?

没有 Pandas

scores = [Decimal('3.7989'),
 Decimal('4.7989'),
 Decimal('5.7989'),
 Decimal('6.7989'),
 Decimal('7.7989')]

 timestamps = [datetime.datetime(2013, 11, 12, 21, 21, 52),
 datetime.datetime(2013, 11, 12, 21, 21, 8),
 datetime.datetime(2013, 11, 12, 21, 21, 1),
 datetime.datetime(2013, 11, 12, 21, 20, 1),
 datetime.datetime(2013, 11, 12, 21, 19, 33)]

 plt.plot(timestamps,score)

enter image description here

使用 Pandas

ts = pd.Series(scores, index=timestamps)
ts.plot()

我们收到错误:TypeError: Series has object dtype and cannot be converted: no numeric data to plot

最佳答案

尝试去掉 Decimal 类型:

ts = pd.Series([float(x) for x in scores], index=timestamps)

ts = pd.Series(scores, index=timestamps, dtype='float64')

Pandas 仅支持 float 和 integer 作为数字类型,使用其他任何类型,它都成为“对象”。

关于python - 能够绘制为列表,无法绘制为 Pandas 系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944124/

相关文章:

python - xml.dom.minidom 通过标记名获取元素

python - 在 Python wheel 中包含 requirements.txt 文件

python - 删除散点图中线下方的数据 (Python)

python - 将 X、Y、Z 值转换为矩阵图

python - matplotlib pdf savefig 提前退出

Python使用装饰器实现上下文管理器

python - 有没有办法用额外的属性来增加 django QuerySets?

python - 从包含键的列表创建字典的键

python - PyUSB dev.set_configuration()

python - 为什么我的@property 装饰器不工作?