6

I created a single columen dataframe filled with np.nan as follows:

df=pd.DataFrame([np.nan]*5)

    0
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN

when I try to look for the data type of df.iloc[0,0], i.e. NaN, the value returns numpy.float64

I know that the pd.isnull function could correctly returns true for these np.NaN. However, I don't understand why the data type is a float?

user7786493
  • 443
  • 3
  • 6
  • 14
  • Numpy defines it that way. `type(pd.np.nan)` is `float` – Zero Feb 21 '18 at 12:52
  • 1
    Possible duplicate of [Why is NaN considered as a float?](https://stackoverflow.com/questions/48558973/why-is-nan-considered-as-a-float) – jpp Feb 21 '18 at 13:02

2 Answers2

3

NaN in numeric arrays

>>> a = float('inf')
>>> b = float('-inf')
>>> c = float('nan')
>>> a
inf
>>> b
-inf
>>> c
nan

NaN values propagate through all operations without raising an exception. For example:

>>> c = float('nan')
>>> c + 23
nan
>>> c / 2
nan
>>> c * 2
nan
>>> math.sqrt(c)
nan
>>>

If it's float type it helps Python during calculation.

CezarySzulc
  • 1,849
  • 1
  • 14
  • 30
2

NumPy (np) uses the IEEE Standard for Binary Floating-Point for Arithmetic calculations (IEEE 754). Thats why when you do any type of manipulation with NumPy it will return Float

>>> type(np.nan*5)
<class 'float'>
MrBhuyan
  • 151
  • 2
  • 17