Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for NaN and using it in an If [duplicate]

I am collecting some data from a database and adding them together to get some statistics, but since I backdate some of my data then the calculated sum will sometime come up as NaN (not a number) I want to create an if sentence that says if(not a number) then exclude this data from my table.

How do I test if the data (in this case double) is NaN?

like image 304
Marc Rasmussen Avatar asked Oct 22 '12 12:10

Marc Rasmussen


People also ask

How do I replace missing values with NaN?

You can replace the missing value ( NaN ) in pandas. DataFrame and Series with any value using the fillna() method.

Does Isnull check for NaN?

Detect missing values for an array-like object. This function takes a scalar or array-like object and indicates whether values are missing ( NaN in numeric arrays, None or NaN in object arrays, NaT in datetimelike).

How do you check if there is duplicate in pandas?

The pandas. DataFrame. duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.


2 Answers

There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.

double x = ... // whatever calculation you do

if (Double.isNaN(x)) {
    ...
}
like image 155
Bill the Lizard Avatar answered Sep 22 '22 01:09

Bill the Lizard


You can test for NaN two ways. You can use the built in function

Double.isNaN(x)

or perform the check this does which is

if (x != x)

provided x is a double or a float

like image 27
Peter Lawrey Avatar answered Sep 25 '22 01:09

Peter Lawrey