【问题标题】:How to iterate over window objects to add them to a DataFrame?如何迭代窗口对象以将它们添加到 DataFrame?
【发布时间】:2019-05-17 18:00:37
【问题描述】:

我有一个对象,它似乎是一个窗口对象EWM [com=9.5,min_periods=0,adjust=True,ignore_na=False,axis=0],它是从数据框predictions_df_list["prices"] 创建的,它是一个以日期为索引、以指数加权平均价格为值的对象。我想将它添加到数据框中:predictions_df_list['ewma']。然而它在推断中引发了NotImplementedError

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-21-b1286fe39d1c> in <module>
---> 59     predictions_df_list['ewma'] = pd.DataFrame.ewm(predictions_df_list["prices"], span=20) #pd.DataFrame.ewma
     60     predictions_df_list['actual_value'] = test['prices']
     61     predictions_df_list['actual_value_ewma'] = pd.DataFrame.ewm(predictions_df_list["actual_value"], span=20)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   3117         else:
   3118             # set column
-> 3119             self._set_item(key, value)
   3120 
   3121     def _setitem_slice(self, key, value):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
   3192 
   3193         self._ensure_valid_index(value)
-> 3194         value = self._sanitize_column(key, value)
   3195         NDFrame._set_item(self, key, value)
   3196 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _sanitize_column(self, key, value, broadcast)
   3385             value = _sanitize_index(value, self.index, copy=False)
   3386 
-> 3387         elif isinstance(value, Index) or is_sequence(value):
   3388             from pandas.core.series import _sanitize_index
   3389 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\inference.py in is_sequence(obj)
    470 
    471     try:
--> 472         iter(obj)  # Can iterate over it.
    473         len(obj)   # Has a length associated with it.
    474         return not isinstance(obj, string_and_binary_types)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\window.py in __iter__(self)
    184     def __iter__(self):
    185         url = 'https://github.com/pandas-dev/pandas/issues/11704'
--> 186         raise NotImplementedError('See issue #11704 {url}'.format(url=url))
    187 
    188     def _get_index(self, index=None):

NotImplementedError: See issue #11704 https://github.com/pandas-dev/pandas/issues/11704

在查找有关窗口对象的文档时,似乎窗口对象是 Python2 对象。无论如何,这里是predictions_df_list["prices"],我正在使用它来重现错误:

2007-11-01    14021.1
2007-11-02    13825.1
2007-11-03    13533.1
2007-11-04    14021.1
2007-11-05    13345.1
2007-11-06    12578.1
2007-11-07    14021.1
2007-11-08    13533.1
2007-11-09    12678.1
2007-11-10    12578.1
2007-11-11    14021.1
2007-11-12    13825.1
2007-11-13    13533.1
2007-11-14    12661.1
2007-11-15    13320.1
2007-11-16    12678.1
2007-11-17    12775.1
2007-11-18    13533.1
2007-11-19    13868.1
2007-11-20    12581.1
2007-11-21    13345.1
2007-11-22    13533.1
2007-11-23    12678.1
2007-11-24    13533.1
2007-11-25    12684.1
2007-11-26    13825.1
2007-11-27    14021.1
2007-11-28    14021.1
2007-11-29    12678.1
2007-11-30    12578.1
               ...   
2007-12-02    13320.1
2007-12-03    12661.1
2007-12-04    13533.1
2007-12-05    12578.1
2007-12-06    13533.1
2007-12-07    13533.1
2007-12-08    14021.1
2007-12-09    12639.1
2007-12-10    12661.1
2007-12-11    13345.1
2007-12-12    12578.1
2007-12-13    14021.1
2007-12-14    13345.1
2007-12-15    13533.1
2007-12-16    12895.1
2007-12-17    13686.1
2007-12-18    14052.1
2007-12-19    14021.1
2007-12-20    13686.1
2007-12-21    12730.1
2007-12-22    13686.1
2007-12-23    12586.1
2007-12-24    12741.1
2007-12-25    12678.1
2007-12-26    13533.1
2007-12-27    12775.1
2007-12-28    12578.1
2007-12-29    12661.1
2007-12-30    12895.1
2007-12-31    12639.1
Freq: D, Name: prices, Length: 61, dtype: float64

【问题讨论】:

    标签: python-3.x dataframe iterable


    【解决方案1】:

    您的 ewma 值可以通过使用您拥有的 EMA 对象并在其上调用 .mean() 来找到。

    df['ewm'] = df['values'].ewm(alpha=0.001).mean()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多