15

I have a number

e.g.

a = 1.22373

type(a) is float

Like wise I want to find if a number is

float64 

or not.

How I will find using Python or NumPy?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
sam
  • 18,509
  • 24
  • 83
  • 116

4 Answers4

38

Use isinstance:

>>> f = numpy.float64(1.4)
>>> isinstance(f, numpy.float64)
True
>>> isinstance(f, float)
True

numpy.float64 is inherited from python native float type. That because it is both float and float64 (@Bakuriu thx for pointing out). But if you will check python float instance variable for float64 type you will get False in result:

>>> f = 1.4
>>> isinstance(f, numpy.float64)
False
>>> isinstance(f, float)
True
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • 2
    Note that `isinstance(f, float)` is `True` when `f` is a `np.float64` because `np.float64` inherits from python's `float`. – Bakuriu Jan 21 '14 at 09:42
  • And in a way that is true as long as one holds to the idea that a `float64` _is_ a `float`. Just stick to that concept. – Alfe Jan 21 '14 at 09:45
  • 4
    more generally you can check for `is_float(x)` via [`isinstance(x, (np.floating, float))`](https://stackoverflow.com/a/28293294/52074) which will check both built-in float and ALL numpy floating point types. i think many people coming to this page are looking to check not just `np.float64` but `np.float{32,64,128}`. – Trevor Boyd Smith Jul 23 '19 at 15:56
2

I find this is the most readable method for checking Numpy number types

import numpy as np
npNum = np.array([2.0]) 

if npNum.dtype == np.float64:
    print('This array is a Float64')

# or if checking for multiple number types:
if npNum.dtype in [
    np.float32, np.float64, 
    np.int8, np.uint8, 
    np.int16, np.uint16, 
    np.int32, np.uint32, 
    np.int64, np.uint64
    ]:

    print('This array is either a float64, float32 or an integer')
DougR
  • 3,196
  • 1
  • 28
  • 29
  • `np.integer` is a generic type, you need to replace that with `np.int32, np.int64, ...` explicitly. – dsz Apr 02 '21 at 02:50
0

If you are comparing numpy types only, it may be better to base your comparison on the number identifying each dtype, which is what the underlying C code does. On my system, 12 is the number for np.float64:

>>> np.dtype(np.float64).num
12
>>> np.float64(5.6).dtype.num
12
>>> np.array([5.6]).dtype.num
12

To use it with non-numpy values also, you could duck-type your way through it with something like:

def isdtype(a, dt=np.float64):
    try:
        return a.dtype.num == np.dtype(dt).num
    except AttributeError:
        return False
Jaime
  • 65,696
  • 17
  • 124
  • 159
0

If you're working with Series or Arrays, also checkout pandas.api.types.is_float_dtype(), which can be applied to Series or on a set of dtypes; e.g.:

dts = df.dtypes # Series of dtypes with the colnames as the index
is_floating = dts.apply(pd.api.types.is_float_dtype)
floating_cols_names = dts[is_floating].index.tolist()

See also:

  • pandas.api.types.is_integer_dtype()
  • pandas.api.types.is_numeric_dtype()

etc.

See https://pandas.pydata.org/pandas-docs/version/1.1.4/reference/api/pandas.api.types.is_bool_dtype.html and following pages.

dsz
  • 4,542
  • 39
  • 35