15

There seems to be a subtle difference between numpy.float and numpy.float64.

>>> import numpy as np
>>> isinstance(2.0, np.float)
True
>>> isinstance(2.0, np.float64)
False

Can someone clarify this? Thanks

2
  • numpy.float is Python's float class. Do you mean numpy.float_ with an underscore at the end? That is the same as numpy.float64.
    – kennytm
    Sep 23, 2016 at 17:45
  • I meant numpy.float without underscore. Would you elaborate your comments, maybe into an answer?
    – zell
    Sep 23, 2016 at 17:46

2 Answers 2

16

np.float is an alias for python float type. np.float32 and np.float64 are numpy specific 32 and 64-bit float types.

float?
Init signature: float(self, /, *args, **kwargs)
Docstring:     
float(x) -> floating point number

Convert a string or number to a floating point number, if possible.
Type:           type

np.float?
Init signature: np.float(self, /, *args, **kwargs)
Docstring:     
float(x) -> floating point number

Convert a string or number to a floating point number, if possible.
Type:           type

np.float32?
Init signature: np.float32(self, /, *args, **kwargs)
Docstring:      32-bit floating-point number. Character code 'f'. C float compatible.
File:           c:\python\lib\site-packages\numpy\__init__.py
Type:           type

np.float64?
Init signature: np.float64(self, /, *args, **kwargs)
Docstring:      64-bit floating-point number. Character code 'd'. Python float compatible.
File:           c:\python\lib\site-packages\numpy\__init__.py
Type:           type

Thus, when you do isinstance(2.0, np.float), it is equivalent to isinstance(2.0, float) as 2.0 is a plain python built-in float type... and not the numpy type.

isinstance(np.float64(2.0), np.float64) would obviously be True.

1
  • I had an arrival delay time that was float64. When I converted the departure delay time type from int64 to match the arrival delay type, I didn't catch that I used fillna().astype('float') and the data/scatter plot changed a lot. I couldn't figure out what was going on. When I corrected it to fillna().astype('float64') all was good. Not sure why it made such a difference when all the values looked like 55.00, 130.00 etc.
    – Edison
    Jun 18, 2022 at 6:25
-6

This would seem to be the difference between a 32-bit floating point number and a 64-bit floating point number (in C, a float vs. a double), and 2.0 ends up being a 32-bit floating point number.

1
  • 3
    Sorry, I have to downvote: Your answer seems wrong. Here is what I just tried: isinstance(1e200, np.float64), which gives false. In addition, isinstance(2.0, np.float32) gives false too.
    – zell
    Sep 23, 2016 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.