【问题标题】:Able to plot as lists, unable to plot as Pandas Series能够绘制为列表,无法绘制为熊猫系列
【发布时间】:2013-11-25 11:37:20
【问题描述】:

以下数据可以使用matplotlib 绘制,但转换为 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)

使用熊猫

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

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

【问题讨论】:

  • 嗯。您是否接受了答案而不投票认为它有用?

标签: python python-2.7 matplotlib pandas


【解决方案1】:

尝试去掉Decimal类型:

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

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

Pandas 只支持 float 和 integer 作为数值类型,使用其他任何东西,它都会变成一个“对象”。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 2019-10-19
    • 2020-03-02
    • 1970-01-01
    • 2014-10-24
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多