0

Intento suavizar la siguiente serie de tiempo

import pandas as pd
import yfinance as yf

symbol = 'BTC-USD'
#df = web.DataReader(symbol, 'yahoo', '2019-01-01', '2019-04-01')
bitcoin = yf.download("^IBEX", start="2019-07-19", end = "2019-08-17")

Para ello utilizo el método savgol_filter

from scipy.signal import savgol_filter

smoothed_2dg = savgol_filter(bitcoin, window_length = 5, polyorder = 2)
smoothed_2dg

smoothed_1dg = savgol_filter(bitcoin, window_length = 5, polyorder = 1)
smoothed_1dg

bitcoin['smoothed_2dg'] = smoothed_2dg
bitcoin['smoothed_1dg'] = smoothed_1dg

Me devuelve el error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2894             try:
-> 2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'smoothed_2dg'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/generic.py in _set_item(self, key, value)
   3573         try:
-> 3574             loc = self._info_axis.get_loc(key)
   3575         except KeyError:

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err
   2898 

KeyError: 'smoothed_2dg'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-4-946476b3b09b> in <module>
      7 smoothed_1dg
      8 
----> 9 bitcoin['smoothed_2dg'] = smoothed_2dg
     10 bitcoin['smoothed_1dg'] = smoothed_1dg

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   3038         else:
   3039             # set column
-> 3040             self._set_item(key, value)
   3041 
   3042     def _setitem_slice(self, key: slice, value):

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/frame.py in _set_item(self, key, value)
   3115         self._ensure_valid_index(value)
   3116         value = self._sanitize_column(key, value)
-> 3117         NDFrame._set_item(self, key, value)
   3118 
   3119         # check if we are modifying a copy

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/generic.py in _set_item(self, key, value)
   3575         except KeyError:
   3576             # This item wasn't present, just insert at end
-> 3577             self._mgr.insert(len(self._info_axis), key, value)
   3578             return
   3579 

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/internals/managers.py in insert(self, loc, item, value, allow_duplicates)
   1187             value = _safe_reshape(value, (1,) + value.shape)
   1188 
-> 1189         block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
   1190 
   1191         for blkno, count in _fast_count_smallints(self.blknos[loc:]):

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype)
   2720         values = DatetimeArray._simple_new(values, dtype=dtype)
   2721 
-> 2722     return klass(values, ndim=ndim, placement=placement)
   2723 
   2724 

~/anaconda3/envs/yfinance/lib/python3.7/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)
    129         if self._validate_ndim and self.ndim and len(self.mgr_locs) != len(self.values):
    130             raise ValueError(
--> 131                 f"Wrong number of items passed {len(self.values)}, "
    132                 f"placement implies {len(self.mgr_locs)}"
    133             )

ValueError: Wrong number of items passed 6, placement implies 1

¿Cuál puede ser la causa?. Agradeceré ayuda.

1 respuesta 1

0

El error radicā en que el primer parámetro que hay que dar al método savgol_filter, NO es un DataFrame, si no una serie. En este caso tenemos que darle bitcoin["Close"]

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.