Ad

Numpy: Np.finfo Does Not Fail As One Would Expect

- 1 answer

I am using numpy.iinfo and np.finfo to test whether a given type or value is corresponds to an integer or a float. There is a weird behaviour when dealing with None.

The following fails, as expected:

 np.iinfo(None)

Nevertheless

 np.finfo(None)

does not, as I would expect. Is that a bug? IMHO when type(None) returns NoneType, then np.finfo should fail as np.iinfo does.

Ad

Answer

On a purely technical level we can explain what you are seeing by looking at the source

The relevant snippet would be

@set_module('numpy')
class finfo(object):

<--snip-->   

    def __new__(cls, dtype):
        try:
            dtype = numeric.dtype(dtype)
        except TypeError:
            # In case a float instance was given
            dtype = numeric.dtype(type(dtype))

The behavior you see is a consequence of np.dtype(None) evaluating to dtype('float64'), which in itself doesn't feel 100% natural to me. Others (including devs) seem to agree. For example

Oh, well, we could probably deprecate dtype(None), which seems pretty useless, but I don't know if it's worth the bother...

Based on this discussion and from the code above the way None is treated does indeed look a bit unintended, but that is just my educated guess.

Ad
source: stackoverflow.com
Ad