计划滚动平均(Pandas)不工作

2024-10-02 18:20:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这里有很多关于熊猫滚动平均函数的问题,但是我不能让它发挥作用。在

我是Python的新手,还有numpy、pandas等包

我有一个日期时间对象列表和一个简单整数列表。绘制它们没有问题。但我不能在图上画出移动平均线!在

我试图用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)

…我得到以下错误:

^{pr2}$

你很快就会知道,我很困惑。我想我错过了系列对象的重点。在

编辑:(完全回溯)

    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.

有人能告诉我如何用滚动平均函数画出移动平均线吗?在


Tags: inpandasdateobjecttimeargwindowrule