1

How can I fix attribute error in this situation? I have a pandas dataframe where I make some data slicing and transformation and I want to plot the results of the persistence_model function like below.

Edit: I want to customize a function with specific title of the plot, y and x axis and create a horizontal line on the same plot from the results of persitence_model function.


class ResidualErrors():
 def __init__(self, data: pd.Series):
 self.data = data
 def _persistence_forecast_model_nrows(self, test_rows):
 slicer = test_rows + 1
 errors = self.data[-slicer:].diff().dropna()
 return errors
 def _persistence_forecast_model_percrows(self, train_perc):
 n = len(self.data)
 ntrain = int(n * train_perc)
 errors = self.data[ntrain:].diff().dropna()
 return errors
 def persistence_model(self, test_rows=None, train_perc=None):
 if (not test_rows) and (not train_perc):
 raise TypeError(r"Please provide 'test_rows' or 'train_perc' arguments.")
 if test_rows and train_perc:
 raise TypeError(r"Please choose one argument either 'test_rows' or 'train_perc'.")
 if test_rows:
 return self._persistence_forecast_model_nrows(test_rows)
 else:
 return self._persistence_forecast_model_percrows(train_perc)
 @classmethod
 def plot_residuals(obj):
 obj.plot()
 plt.show()

Desired output

res = ResidualErrors(data).persistence_model(test_rows=10)
res.plot_residuals()
>> AttributeError: 'Series' object has no attribute 'plot_residuals'
asked Jun 28, 2022 at 9:48
8
  • What are you trying to call obj.plot() on? Do Pandas Series have a .plot() method? I'm not sure. Here your persistence_model method returns a Series, which you would then have to pass to ResidualErrors.plot_residuals. Commented Jun 28, 2022 at 9:52
  • 1
    persistence_model returns a Series. A series does not have a method plot_residuals. Commented Jun 28, 2022 at 9:52
  • I want to integrate the plotting inside the class so I can customize the title, and different matplotlib parameters. Commented Jun 28, 2022 at 9:53
  • @matt.aurelio ok, but I don't know why you expect a @classmethod to work that way. obj will be passed the class, i.e ResidualErrors. It's not clear why you expected res.plot_residuals() to work since persistence_model returns a pandas.Series object Commented Jun 28, 2022 at 9:57
  • 1
    You could change class method to def plot_residuals(cls, ser): ser.plot();plt.show(). Then call with ResidualErrors.plot_residuals(res) Commented Jun 28, 2022 at 10:03

1 Answer 1

1

You need to be more aware of what methods return. The first step creates a ResidualErrors object:

res = ResidualErrors(data)

The second step creates a DataFrame or Series:

obj = res.persistence_model(test_rows=10)

You can call plot_residuals on res but not on obj, as you are currently doing:

res.plot_residuals(obj)
answered Jun 28, 2022 at 17:29
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.