【问题标题】:Pandas "TypeError: unsupported operand type(s) for +: 'Timedelta' and 'float'"Pandas“TypeError:+ 的不支持的操作数类型:'Timedelta' 和'float'”
【发布时间】:2017-10-02 15:19:11
【问题描述】:

我正在尝试使用带有 sql 结果填充数据框的 pandas autocorrelation_plot() 函数,但是当我运行此代码时,我不断收到此错误:

import MySQLdb
import pandas as pd
from matplotlib import pyplot
from pandas.tools.plotting import autocorrelation_plot
connection = MySQLdb.connect(host = "host", user = "user", passwd = "passwd", db = "db")
exect = connection.cursor()
query = "query"
exect.execute(query)
frame = pd.read_sql(query, con=connection)
connection.close()
print(frame.head())
autocorrelation_plot(frame)
pyplot.show()

我可以毫无问题地打印框架,但是当我尝试使用 autocorrelation_plot() 函数时出现此错误。我的数据框的输出是这样的:

time      value
0 00:00:14  283.80
1 00:01:14  271.97
2 00:02:14  320.53
3 00:03:14  346.78
4 00:04:14  280.72
5 00:05:14  277.41
6 00:06:14  308.65
7 00:07:14  321.27
8 00:08:14  320.68
9 00:09:14  332.32

【问题讨论】:

  • 你知道你要做什么吗?
  • 当我打印帧的值时,我正在尝试从看起来像这样的时间序列绘制自相关:0 00:00:14 283.80 1 00:01:14 271.97 2 00:02:14 320.53 3 00:03:14 346.78 4 00:04:14 280.72
  • 打印df.head(10)并在此处发布。

标签: python pandas matplotlib dataframe plot


【解决方案1】:

您可能会受益于使用set_indextime 设置为绘图前的索引。

from pandas.plotting import autocorrelation_plot

df    
      time   value
0 00:00:14  283.80
1 00:01:14  271.97
2 00:02:14  320.53
3 00:03:14  346.78
4 00:04:14  280.72
5 00:05:14  277.41
6 00:06:14  308.65
7 00:07:14  321.27
8 00:08:14  320.68
9 00:09:14  332.32

df.dtypes     
time     timedelta64[ns]
value            float64
dtype: object

autocorrelation_plot(df.set_index('time'))
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2017-06-06
    • 2018-10-30
    • 2014-03-28
    • 2018-06-20
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多