Cycle fordá results Nan! How to get real values?

0

One of the parts of my script is to determine the area of a graph by the trapezoid approximation. Within the class I created the following function that handles the self.df, which is a DataFrame

def areas(self):
    area = 0.
    x = list(self.df['wavelenght'])
    y = list(self.df['R'])

    tamanho = len(self.df)

    for i in range(tamanho):
        if i == tamanho - 1:
            break

        x0 = x[i]
        y0 = y[i]

        x1 = y[i+1]
        y1 = y[i+1]

        h = x1 - x0
        B = y1 + y0

        area += (B/2.)*h
        print(area)
    return area

The line that has print (area) serves to follow the area value in each for loop. In the output text, after several lines with float values comes a time when this occurs

0.514712375
0.517317875
0.51993125
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan

Manually I get the values that these nan should get but I want to manipulate more than 100 DataFrames.

Why are you giving me these exits? What is the best solution?

    
asked by anonymous 23.07.2017 / 11:29

1 answer

0

nan , means: "Not a number", that is, a floating point value that you get when you run a calculation whose result can not be expressed as a number, any calculation that you execute with nan , will result in nan . I suggest you change the for , added before print(area) as follows:

print('Valor de b: ', b)
print('valor de h:', h)

Then go to a python console, get the values of b and h (in the lines where the results were nan ) and see if they are not nan :

import math
print (math.isnan(value))  

Or add these lines in the for loop itself.

You should check tb (in the console, because the loop is.) if the equation to generate the new area ( area += (B/2.)*h ), is not generating a nan .

If you find nan in these variables, you probably have some error in your algorithm or dataframe data, check them.

    
23.07.2017 / 22:00