Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.int64 is not instance of int #2951

Closed
dmvianna opened this issue Jan 27, 2013 · 4 comments
Closed

numpy.int64 is not instance of int #2951

dmvianna opened this issue Jan 27, 2013 · 4 comments

Comments

@dmvianna
Copy link

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.

@njsmith
Copy link
Member

njsmith commented Jan 27, 2013

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 notifications@github.comwrote:

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][<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.


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

@pv
Copy link
Member

pv commented Jan 29, 2013

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

@seberg
Copy link
Member

seberg commented Jan 29, 2013

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.

@charris
Copy link
Member

charris commented Mar 25, 2014

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

@charris charris closed this as completed Mar 25, 2014
yashu-seth added a commit to yashu-seth/pgmpy that referenced this issue May 2, 2016
This bug occurs because numpy does not take numpy.int64 to be an instance
of int. For more details refer -
numpy/numpy#2951
yashu-seth added a commit to yashu-seth/pgmpy that referenced this issue May 3, 2016
This bug occurs because numpy does not take numpy.int64 to be an instance
of int. For more details refer -
numpy/numpy#2951
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants