Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between np.nan and np.NaN

Tags:

arrays

nan

numpy

Is there any difference between np.Nan and np.nan? As per my understanding both are used for null values but if you look here

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame([[np.nan,2,np.nan,0],[3,4,np.nan,1],[np.nan,np.nan,np.nan,5]],columns=list('ABCD'))
print(df)
print(np.nan == np.NaN)

I get following output:

     A    B   C  D
0  NaN  2.0 NaN  0
1  3.0  4.0 NaN  1
2  NaN  NaN NaN  5
False

Process finished with exit code 0

Now if these are same print(np.nan == np.NaN) should return True and why are the values in dataframe populated as NaN?

I get NaN is not a number so it might be treating it that way and hence changing the entry in dataframe but I am still not sure.

like image 247
user10089194 Avatar asked Nov 22 '18 18:11

user10089194


People also ask

Is NP NaN same as NP NaN?

notnull to test for missing data (NaN). np. nan is not comparable to np. nan ...

What is NP NaN in NumPy?

In Python, NumPy NAN stands for not a number and is defined as a substitute for declaring value which are numerical values that are missing values in an array as NumPy is used to deal with arrays in Python and this can be initialized using numpy.

Is NaN and NaN same?

NaN is not equal to NaN! Short Story: According to IEEE 754 specifications any operation performed on NaN values should yield a false value or should raise an error.

How do you compare NP and NaN?

To check for NaN values in a Numpy array you can use the np. isnan() method. This outputs a boolean mask of the size that of the original array. The output array has true for the indices which are NaNs in the original array and false for the rest.


2 Answers

so basically NaN,NAN and nan are equivalent definitions of nan

or in other words

NaN and NAN are aliases of nan

np.nan
np.NaN
np.NAN

if you will check the equality of these it returns False

and if you check the types of all these 3 then you will find that all are of same type(float)

but let

a=np.NaN
b=np.NAN
c=np.nan

now if you will check the equality of a,b and c it returns True

Even in the documentation(line 4) it is said that:-

cannot use equality to test NaNs

you can check the documentation from here:-

https://numpy.org/doc/stable/user/misc.html?highlight=numpy%20nan

like image 144
Anurag Dabas Avatar answered Oct 11 '22 03:10

Anurag Dabas


actually even if you test: np.nan == np.nan you would get false

like image 1
salama4ai Avatar answered Oct 11 '22 02:10

salama4ai