绘制rolling_mean(熊猫)不起作用

时间:2014-03-03 13:50:36

标签: python numpy pandas

我知道这里有很多关于在pandas中使用rolling_mean函数的问题 - 但我无法让它工作。

我是Python新手,包numpy,pandas等。

我有一个DateTime对象列表,我有一个简单整数列表。绘制它们没有问题。但我无法在图表上绘制移动平均线!

我正在尝试使用Pandas docs来理解它,但我仍然无法让它发挥作用。这就是我的尝试:

# code before this simply reads a file and converts dates into datetime objects and values into a list
x1 = date_object #list of datetime objects
y1 = values1 #list of integer values
plot(x1,y1) # works fine

t = pd.date_range(date_object[0].strftime('%m/%d/%Y'), date_object[len(date_object)-1].strftime('%m/%d/%Y'), freq='W')
ts = pd.Series(y1, t)

ts_movavg = pd.rolling_mean(ts,10)

plot(ts_movavg)

..我收到以下错误:

ValueError: setting an array element with a sequence.

你可能很快就会说,我很困惑。我想我错过了Series对象的观点。

编辑:(完全追溯)

    ValueError                                Traceback (most recent call last)
<ipython-input-228-2247062d3126> in <module>()
     33 ts = pd.Series(y1, x1)
     34 
---> 35 ts_movavg = PD.rolling_mean(ts,10)
     36 
     37 ts_movavg.head()

C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in f(arg, window, min_periods, freq, center, time_rule, **kwargs)
    507         return _rolling_moment(arg, window, call_cython, min_periods,
    508                                freq=freq, center=center,
--> 509                                time_rule=time_rule, **kwargs)
    510 
    511     return f

C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in _rolling_moment(arg, window, func, minp, axis, freq, center, time_rule, **kwargs)
    278     arg = _conv_timerule(arg, freq, time_rule)
    279     calc = lambda x: func(x, window, minp=minp, **kwargs)
--> 280     return_hook, values = _process_data_structure(arg)
    281     # actually calculate the moment. Faster way to do this?
    282     if values.ndim > 1:

C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in _process_data_structure(arg, kill_inf)
    326 
    327     if not issubclass(values.dtype.type, float):
--> 328         values = values.astype(float)
    329 
    330     if kill_inf:

ValueError: setting an array element with a sequence.

有人可以告诉我如何使用rolling_mean函数绘制移动平均线吗?

0 个答案:

没有答案