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

Viewed 24

I am running the following Python code where I need to append the results (PwD an array) to an empty array (P) to save and export them to an Excel file. However, the message 'numpy.float64' object has no attribute 'append' is said always. I have no idea how to fix it. Your help is really appreciated.

import math
import plotly.express as px
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pylab as ax
import xlsxwriter



FcD = 10.0     
RcD = 100000.0 
w = 500.0      
Lam = 0.002     
xeD =2.0       
yeD = 1.0     
wD = 0.0002    
noD = 1*10**(-6) 
nfD = 1000.0     


P = []
TD = []
for i in range(-8, 9, 1):
    for tD in np.arange(1.0 * 10**(i),9.7 * 10**(i),1.0 * 10**(i)):
        Pd = []
        for j in range(1,11,1):
            s = j * np.log(2.0) / tD   # s is Laplace operator 
            u = s * (1+(Lam * w / (3 * s))**(0.5) * np.tanh((3 * s * w / Lam)**(0.5)))
            Bo = (s / noD)**(0.5) * np.tanh((s / noD) * (xeD - 1))
            Ao = Bo / (RcD * yeD) + u
            Bf = (Ao)**(0.5) * np.tanh((Ao)**(0.5) * (yeD - wD / 2))
            Af = 2 * Bf / FcD + s / nfD
            P = 3.14 / (FcD * s * (Af)**(0.5) * np.tanh((Af)**(0.5)))
            Pd.append(P)
        PDd = np.array([Pd])
        PD = PDd.flatten()
        v = np.array([0.0833333, -32.083333, 1279, -15623.667, 84244.1667, -236957.5, 375911.667, -340071.67, 164062.5, -32812.5])
        PwD = np.log(2) / tD * np.dot(v, PD)
        P.append(PwD)
1 Answers

In the innermost for loop you assigned a float value to P

P = 3.14 / (FcD * s * (Af)**(0.5) * np.tanh((Af)**(0.5)))

This changes P from an array to a float type as the value of 3.14 / (FcD * s * (Af)**(0.5) * np.tanh((Af)**(0.5))) is a float. And then you try to append to this float thus the error. You could solve this by using P.append(3.14 / (FcD * s * (Af)**(0.5) * np.tanh((Af)**(0.5)))) or simply use indexing to assign the value P[i] = 3.14 / (FcD * s * (Af)**(0.5) * np.tanh((Af)**(0.5)))

Hope this helps.

Related