Skip to content

AttributeError: 'Series' object has no attribute 'freq' (DeepAR algorithm in Time series forecasting)  #1845

Closed

Description

@sylviahangnguyen

Describe the bug
I'm running dbg-deepar.ipynb, provided module deepar_util.py by following the tutorial
Stock Price Prediction, using SageMaker DeepAR
.
The problem may be that I cannot update Pandas on SageMaker Studio Notebook. My Pandas version '1.0.1'

class DeepARPredictor in deepar_util.py

class DeepARPredictor(sagemaker.predictor.RealTimePredictor):
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, content_type=sagemaker.content_types.CONTENT_TYPE_JSON, **kwargs)
        
    def predict(self, ts, cat=None, dynamic_feat=None, 
                num_samples=100, return_samples=False, quantiles=["0.1", "0.5", "0.9"]):
        """Requests the prediction of for the time series listed in `ts`, each with the (optional)
        corresponding category listed in `cat`.
        
        ts -- `pandas.Series` object, the time series to predict
        cat -- integer, the group associated to the time series (default: None)
        num_samples -- integer, number of samples to compute at prediction time (default: 100)
        return_samples -- boolean indicating whether to include samples in the response (default: False)
        quantiles -- list of strings specifying the quantiles to compute (default: ["0.1", "0.5", "0.9"])
        
        Return value: list of `pandas.DataFrame` objects, each containing the predictions
        """
        prediction_time = ts.index[-1] + pd.Timedelta(1, unit='D')
        quantiles = [str(q) for q in quantiles]
        req = self.__encode_request(ts, cat, dynamic_feat, num_samples, return_samples, quantiles)
        res = super(DeepARPredictor, self).predict(req)
        return self.__decode_response(res, ts.index.freq, prediction_time, return_samples)
    
    def __encode_request(self, ts, cat, dynamic_feat, num_samples, return_samples, quantiles):
        instance = series_to_dict(ts, cat if cat is not None else None, dynamic_feat if dynamic_feat else None)

        configuration = {
            "num_samples": num_samples,
            "output_types": ["quantiles", "samples"] if return_samples else ["quantiles"],
            "quantiles": quantiles
        }
        
        http_request_data = {
            "instances": [instance],
            "configuration": configuration
        }
        
        return json.dumps(http_request_data).encode('utf-8')
    
    def __decode_response(self, response, freq, prediction_time, return_samples):
        # we only sent one time series so we only receive one in return
        # however, if possible one will pass multiple time series as predictions will then be faster
        predictions = json.loads(response.decode('utf-8'))['predictions'][0]
        prediction_length = len(next(iter(predictions['quantiles'].values())))
        prediction_index = pd.DatetimeIndex(start=prediction_time, freq=freq, periods=prediction_length)        
        if return_samples:
            dict_of_samples = {'sample_' + str(i): s for i, s in enumerate(predictions['samples'])}
        else:
            dict_of_samples = {}
        return pd.DataFrame(data={**predictions['quantiles'], **dict_of_samples}, index=prediction_index)

    def set_frequency(self, freq):
        self.freq = freq    

Function in dbg-deepar.ipynb

predictor = DeepARPredictor(estimator_job)

ts, dynamic_feat, observed = util.query_for_stock('BMW', target_column, covariate_columns, stock_data_series, prediction_length)

prediction = predictor.predict(ts=ts, dynamic_feat = dynamic_feat, quantiles=[0.10, 0.5, 0.90], return_samples=False)

** Error logs**

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-118-6bae3056850a> in <module>
----> 1 prediction = predictor.predict(ts=ts, dynamic_feat = dynamic_feat, quantiles=[0.10, 0.5, 0.90], return_samples=False)
      2 prediction.head()

~/deepar_util.py in predict(self, ts, cat, dynamic_feat, num_samples, return_samples, quantiles)
    201 
    202         Return value: list of `pandas.DataFrame` objects, each containing the predictions
--> 203         """
    204         prediction_time = ts.index[-1] + pd.Timedelta(1, unit='D')
    205         quantiles = [str(q) for q in quantiles]

/opt/conda/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
   5275 
   5276     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'freq'

System information

  • SageMaker Python SDK version:
  • Framework name (eg. PyTorch) or algorithm (eg. KMeans):
  • Framework version:
  • Python version: 3.7.7
  • CPU or GPU: CPU
  • Custom Docker image (Y/N): N
  • **Pandas version 1.0.1

Activity

  1. ajaykarpur commented on Aug 20, 2020

    @ajaykarpur
    Contributor

    Hi @fallingstar3107, thanks for reaching out. The example repository you linked looks to be unmaintained. Could you please try one of these examples instead?

    Please let us know if you run into any issues or have further questions.

  2. locked and limited conversation to collaborators on May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions