Search code examples
numpypandasdataframesqrt

'float' object has no attribute 'arctan'


  1. i have data as below.

     data                                                            
     62600.0    
     63000.0    
     62900.0    
     60400.0    
     59800.0
    
  2. code is

       def Simplify(G_x,data):
           data['dx'] = data*G_x 
           data['dy'] = data - data.shift(1)
           data = data.fillna(0)
           data['G_mag'] = np.sqrt((data['dx']+data['dy'])*(data['dx']+data['dy'])-2*data['dx']*data['dy']) 
           data['Theta'] = np.arctan(data['dy']/data['dx'])
           data = data.fillna(0) 
           data['Theta']=0
           data.loc[data['Theta']<0,'Theta'] = data['Theta']+2*np.pi
           data.loc[(data['Theta']<=np.pi/8) & (data['Theta']>=0), 'Theta'] = np.arctan(0)
           data.loc[(data['Theta']<=3*np.pi/8) & (data['Theta']>1*np.pi/8), 'Theta'] = np.arctan(1)
           data.loc[(data['Theta']<=np.pi) & (data['Theta']>3*np.pi/8), 'Theta'] = np.arctan(2)
           data.loc[(data['Theta']<=13*np.pi/8) & (data['Theta']>np.pi), 'Theta'] = np.arctan(-2)
           data.loc[(data['Theta']<=15*np.pi/8) & (data['Theta']>13*np.pi/8), 'Theta'] = np.arctan(-1)
           data.loc[(data['Theta']>15*np.pi/8) & (data['Theta']<=2*np.pi), 'Theta'] = np.arctan(0)
           data['New_dy'] = data['dx']*np.tan(data['Theta'])
           data['N_AC']=data['New_dy'].cumsum()+data['Adj Close'][0]
           return data['N_AC']
    

3.Error message is

           'float' object has no attribute 'arctan'
           'float' object has no attribute 'sqrt'

4. When i run this code directly,not using function. it works well. is there any good way to solve this problem. thanks!


Solution

  • You might have created a global variable called np somewhere, so that when you write np.sqrt or np.arctan it tries to find the corresponding methods of the object np instead of calling numpy functions.

    Or as it works when you don't use it as a function, it must be because it doesn't understand in the function the np means the package numpy. If you are working on notebooks such as jupyter, just add import numpy as np in the notebook you're function is in. If you only load numpy in the notebook from which you call the function, it will not understand what np refers to.