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
  • 56,955
  • 33
  • 144
  • 158
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
  • 1,115
  • 1
  • 10
  • 23
3

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

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

Stefan
  • 1,697
  • 15
  • 31
Anurag Dhadse
  • 1,722
  • 1
  • 13
  • 26