0

In the following examples, I create four types of 'nan' in Python. df in a is a data frame I loaded from a .txt file where one missing values sit on [2,2].

a = df.iloc[2,2]
b = pd.NA
c = np.nan
d = float('nan')

First, I had a look at these four values:

a
Out[122]: nan
b
Out[123]: <NA>
c
Out[124]: nan
d
Out[125]: nan

It looks like a,c and d are equal nan values, And I decided to check their type:

type(a)
Out[116]: numpy.float64
type(b)
Out[117]: pandas._libs.missing.NAType
type(c)
Out[118]: float
type(d)
Out[119]: float

I want to check if they are equal in a sense:

c == d
Out[120]: False
a == d
Out[121]: False
a == c
Out[126]: False

My question is:

[1] Why c and d are not equal?

[2] Is there a way to convert float('nan') to np.nan?

[3] Is there a way to convert a to np.nan?

Many thanks!

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • 3
    Because `float('nan') != float('nan')`, this is one of the properties of `nan` – juanpa.arrivillaga Jan 26 '21 at 03:57
  • 1
    "Is there a way to convert float('nan') to np.nan?" they *are the same type of object*, they are both `float` objects. `numpy` just has a `nan` assigned in it's module namespace. Just like if you did `nan = float('nan')` in your own module. – juanpa.arrivillaga Jan 26 '21 at 03:58
  • 1
    "Is there a way to convert a to np.nan?" `a` is a `numpy.float64` scalar, you can just use `float(a)`, like any other `numpy.float64` scalar – juanpa.arrivillaga Jan 26 '21 at 04:00
  • So note, you only really have *three* types of nan here, – juanpa.arrivillaga Jan 26 '21 at 04:01
  • @juanpa.arrivillaga no, there really are 4. `c is d` results in `False`. – Mark Ransom Jan 26 '21 at 04:07
  • @MarkRansom So what? If I have `a = 900` and `b = 900` then `a is b` is false, and yet, there is still only one kind of 900 – juanpa.arrivillaga Jan 26 '21 at 04:13
  • @juanpa.arrivillaga I always thought `float('nan')` was a singleton, but I did some additional testing and discovered that assumption was false. So now I must confess that I really don't know what's going on. – Mark Ransom Jan 26 '21 at 04:19
  • 1
    @MarkRansom `np.nan` is just a reference to an instance of `float` with the value `nan`, it isn't a `np.float64('nan')` or `np.float32('nan')` as one might expect. – juanpa.arrivillaga Jan 26 '21 at 04:25
  • 1
    A bit of history: https://stackoverflow.com/a/1573715/1566221. It's so cool that you can find answers like this on SO. – rici Jan 26 '21 at 04:38

0 Answers0