Python and Numpy nan and set

I came across unpredictable behavior with Python Numpy, set and NaN (not-a-number):

>>> set([np.float64('nan'), np.float64('nan')]) set([nan, nan]) >>> set([np.float32('nan'), np.float32('nan')]) set([nan, nan]) >>> set([np.float('nan'), np.float('nan')]) set([nan, nan]) >>> set([np.nan, np.nan]) set([nan]) >>> set([float('nan'), float('nan')]) set([nan, nan]) 

Here np.nan gives one set of elements, while Numpy nans gives several nans in a set. So swims ("nan")! And note that:

 >>> type(float('nan')) == type(np.nan) True 

It is interesting how this difference arises and what rationality is behind different behaviors.

+5
source share
1 answer

One of the properties of NAN is that NAN! = NAN, unlike all other numbers. However, the set implementation first checks if id (x) matches an existing member in the hash index before it tries to insert a new one. If you have two objects with different identifiers that have a NAN value, you will get two entries in the set. If both of them have the same identifier, they are collapsed into one record.

As pointed out by others, np.nan is the only object that will always have the same identifier.

+5
source

All Articles