numpy.int64 is not instance of int

Created on 27 Jan 2013  Â·  4Comments  Â·  Source: numpy/numpy

As reported in xlwt:

Here is an examination of numpy behaviour (Python 2.7.3, numpy 1.6.2)

>>> import numpy
>>> data = [t(123456) for t in (numpy.int32, numpy.int64, numpy.float64)]
>>> [type(d) for d in data]
[<type 'numpy.int32'>, <type 'numpy.int64'>, <type 'numpy.float64'>]
>>> data
[123456, 123456, 123456.0]
>>> check_types = (int, long, float)
>>> for d in data:
...     for c in check_types:
...         print type(d), repr(c), isinstance(d, c)
...
<type 'numpy.int32'> <type 'int'> True
<type 'numpy.int32'> <type 'long'> False
<type 'numpy.int32'> <type 'float'> False
<type 'numpy.int64'> <type 'int'> False
<type 'numpy.int64'> <type 'long'> False
<type 'numpy.int64'> <type 'float'> False
<type 'numpy.float64'> <type 'int'> False
<type 'numpy.float64'> <type 'long'> False
<type 'numpy.float64'> <type 'float'> True
>>>

Looks like numpy has done the work to make its int32 and float64 recognisable by other software but not int64.

Proposal numpy.core

Most helpful comment

This is correct -- python 'int' is either 32 or 64 bit (depending on your
build; you're using a 32-bit python), so either np.int32 or np.int64
derives from it. np.float32 isn't an instance of 'float', either, because
similarly Python 'float' is specifically stored in double-precision.

(Really even this is silly because numpy ints _aren't_ instances of 'int'
or 'float', but whatever.)

isinstance(..., int) is the wrong tool for what you're trying to do. If you
want to detect integers in general using numpy types, the simplest thing to
do is isinstance(..., np.integer).

On Sat, Jan 26, 2013 at 8:15 PM, Daniel Vianna [email protected]:

As reported in xlwt https://github.com/python-excel/xlwt/issues/15:

Here is an examination of numpy behaviour (Python 2.7.3, numpy 1.6.2)

import numpy>>> data = [t(123456) for t in (numpy.int32, numpy.int64, numpy.float64)]>>> [type(d) for d in data][, , ]>>> data[123456, 123456, 123456.0]>>> check_types = (int, long, float)>>> for d in data:... for c in check_types:... print type(d), repr(c), isinstance(d, c)... True False False False False False False False True>>>

Looks like numpy has done the work to make its int32 and float64
recognisable by other software but not int64.

—
Reply to this email directly or view it on GitHubhttps://github.com/numpy/numpy/issues/2951.

All 4 comments

This is correct -- python 'int' is either 32 or 64 bit (depending on your
build; you're using a 32-bit python), so either np.int32 or np.int64
derives from it. np.float32 isn't an instance of 'float', either, because
similarly Python 'float' is specifically stored in double-precision.

(Really even this is silly because numpy ints _aren't_ instances of 'int'
or 'float', but whatever.)

isinstance(..., int) is the wrong tool for what you're trying to do. If you
want to detect integers in general using numpy types, the simplest thing to
do is isinstance(..., np.integer).

On Sat, Jan 26, 2013 at 8:15 PM, Daniel Vianna [email protected]:

As reported in xlwt https://github.com/python-excel/xlwt/issues/15:

Here is an examination of numpy behaviour (Python 2.7.3, numpy 1.6.2)

import numpy>>> data = [t(123456) for t in (numpy.int32, numpy.int64, numpy.float64)]>>> [type(d) for d in data][, , ]>>> data[123456, 123456, 123456.0]>>> check_types = (int, long, float)>>> for d in data:... for c in check_types:... print type(d), repr(c), isinstance(d, c)... True False False False False False False False True>>>

Looks like numpy has done the work to make its int32 and float64
recognisable by other software but not int64.

—
Reply to this email directly or view it on GitHubhttps://github.com/numpy/numpy/issues/2951.

Also, on Python 3, none of Numpy's integer types is related to the native int type (which is an variable-size integer).

I think Nathaniel is completely correct in his note on the original report. Numpy should inherit/register numbers.Integral etc. which fits well into the dropping of python 2.4 and 2.5. If anyone wants a broad recognition of integer likes, they should be checking for numbers.Integral (or maybe try the similar duck typing __index__ method) anyway.

As of #4547 and 1.9.0, numpy registers numbers with the number module. Closing this as related.

Was this page helpful?
0 / 5 - 0 ratings