Cycle gives results Nan! How to get real values?

Asked

Viewed 106 times

0

One of the parts of my script aims to determine the area of a graph by trapezoid approximation. Within the class I created the following function that manipulates 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 cycle. 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

I can manually get the values 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?

  • tamanho = len(self.df) would not be tamanho = len(self.df['R']) ?

  • Both will give the same value, in this case it is the number of lines that the dataframe has

1 answer

0

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

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

Then go to a python console, take the values of b and h (on the lines where the results were nan) and see if they’re not nan:

import math
print (math.isnan(value))  

Or add these lines in the loop itself.

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

Meet nan in these variables, you probably have some error either in your algorithm or in the dataframe data, check them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.