'numpy.ndarray' object has no attribute 'append'

+1 vote

This is part of my code, why it shows error. thanks in advance

prets = []
pvols = []
for p in range (2500):
    weights = np.random.random(noa)
    weights /= np.sum(weights)
    prets.append(np.sum(rets.mean() * weights) * 252)
    pvols.append(np.sqrt(np.dot(weights.T,
                                np.dot(rets.cov() * 252, weights))))
    prets = np.array(prets)
    pvols = np.array(pvols)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-220-3a3ff7a7f33e> in <module>
      4     weights = np.random.random(noa)
      5     weights /= np.sum(weights)
----> 6     prets.append(np.sum(rets.mean() * weights) * 252)
      7     pvols.append(np.sqrt(np.dot(weights.T,
      8                                 np.dot(rets.cov() * 252, weights))))

AttributeError: 'numpy.ndarray' object has no attribute 'append'
May 17 in Python by Noman
• 220 points

edited May 18 by Gitika 327 views
Can you share your complete code? It is difficult to understand the flow of your code.

1 answer to this question.

0 votes

Hi@Noman,

You created two empty list. And in the for loop you tried to append values in your list. But in the last part you tried to convert your list into numpy array using the same variable name. So I suggest you to change your variable name as given below.

prets = []
pvols = []
for p in range (2500):
    weights = np.random.random(noa)
    weights /= np.sum(weights)
    prets.append(np.sum(rets.mean() * weights) * 252)
    pvols.append(np.sqrt(np.dot(weights.T,
                                np.dot(rets.cov() * 252, weights))))
    pret = np.array(prets)
    pvol = np.array(pvols)

Make this changes in your code and try once again.

answered May 17 by MD
• 24,500 points

Related Questions In Python

0 votes
1 answer

AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

Hi@akhtar, I think numpy version is not compatible ...READ MORE

answered Apr 13 in Python by MD
• 24,500 points
195 views
0 votes
1 answer

Getting AttributeError: 'module' object (scipy) has no attribute 'misc' in Python. How to solve this?

>>> import scipy >>> scipy.misc Traceback (most recent call ...READ MORE

answered Dec 24, 2018 in Python by Nymeria
• 3,540 points
3,069 views
0 votes
0 answers

'module' object has no attribute 'PortScanner'

I am trying to run nmap in ...READ MORE

Jan 30, 2019 in Python by Karan
536 views
0 votes
1 answer
0 votes
1 answer

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

There is no attribute called “rows”. The ...READ MORE

answered Mar 28, 2019 in Python by Shri
28,788 views
0 votes
1 answer

Python error "AttributeError: '_Screen' object has no attribute 'mainloop'" python module turtle

Hey @Nagya, replace  wn.mainlopp() with turtle.mainloop() ...READ MORE

answered Jun 19, 2019 in Python by Faiza
987 views
+1 vote
1 answer

Python error "AttributeError: 'Turtle' object has no attribute 'Shape'"

Hey @Nagya, replace python.Shape("Square") with the following: python.shape("square") Python is case ...READ MORE

answered Jun 19, 2019 in Python by Faiza
5,597 views
0 votes
1 answer

Python error "AttributeError: 'str' object has no attribute 'casefold'"

Check your python version. Casefold is possible ...READ MORE

answered Jul 4, 2019 in Python by Yesha
2,522 views
0 votes
1 answer
+1 vote
1 answer