5

Could you guys please help me explain the code below:

Why a nan is not np.nan?

import pandas as pd
import numpy as np

df.iloc[31464]['SalesPersonID']
[out]:
nan

df.iloc[31464]['SalesPersonID'] is np.nan
[out]:
False

Thank you, all.

Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
Vinh Nguyen
  • 61
  • 1
  • 4

2 Answers2

7

np.nan is a special value in numpy. Read here for more information on it.

The link above mentions the following code snippet:

>>> np.nan == np.nan  # is always False! Use special numpy functions instead.

Also, type(df.iloc[31464]['SalesPersonID']) is np.float64.

Algebra8
  • 825
  • 1
  • 6
  • 19
3

use np.isnan(np.nan) which gives True or

np.isnan(df.iloc[31464]['SalesPersonID']) which gives True

Stefan
  • 1,393
  • 11
  • 25
Anurag Dhadse
  • 1,284
  • 12
  • 19