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

Python 3.7 can cast object to complex type #11993

Closed
bear24rw opened this Issue Sep 19, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@bear24rw
Copy link

bear24rw commented Sep 19, 2018

Reproducing code example:

import numpy as np
print(np.__version__)
arr = np.array(['AAAAA', 18465886.0, 18465886.0], dtype=object)
print(arr.astype(np.complex64))
print(arr.astype(np.complex64)) 

Error message:

Python3.6 works as I would expect (complains that it cannot convert a string to complex dtype)

$ python3.6 test.py 
1.16.0.dev0+7fcba41
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    print(arr.astype(np.complex64))
TypeError: must be real number, not str

Python3.7 converts it to a complex type and then errors if you try to do it again

$ python3.7 test.py 
1.16.0.dev0+7fcba41
[-6.3090155e-31+4.558e-41j  1.8465886e+07+0.000e+00j
  1.8465886e+07+0.000e+00j]
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print(arr.astype(np.complex64)) 
TypeError: must be real number, not str

Numpy/Python version information:

>>> import sys
>>> sys.version
'3.7.0b3 (default, Mar 30 2018, 04:35:22) \n[GCC 7.3.0]'
>>> import numpy as np
>>> np.__version__
'1.16.0.dev0+7fcba41'
@eric-wieser

This comment has been minimized.

Copy link
Member

eric-wieser commented Sep 20, 2018

Do you get this for np.complex32 too?

I'm suspicious this might relate to #11998

@jjhelmus

This comment has been minimized.

Copy link
Contributor

jjhelmus commented Sep 20, 2018

I was able to replicate this will various complex data types including np.complex64, np.complex128 and np.complex256.

@hhuuggoo

This comment has been minimized.

Copy link

hhuuggoo commented Sep 30, 2018

I don't think it is related to #11998. The reason I say that is because I have 2 environments (py36 and numpy 1.15.2) and (py37 and numpy 1.15.2)

in both I get

>>> (issubclass(np.float_, float),  issubclass(np.float_, np.floating))
(True, True)
>>> (issubclass(np.complex_, float),  issubclass(np.complex_, np.complexfloating))
(False, True)

ahaldane added a commit to ahaldane/numpy that referenced this issue Sep 30, 2018

ahaldane added a commit to ahaldane/numpy that referenced this issue Sep 30, 2018

@ahaldane

This comment has been minimized.

Copy link
Member

ahaldane commented Sep 30, 2018

Should be fixed by #12062. It was a missing error check, which caused the error to be cleared by subsequence cast operations. That's why

>>> np.array(['AAAAA', 18465886.0, 18465886.0], dtype=object).astype('c8')

fails to raise an error but

>>> np.array(['AAAAA', 'BBBB'], dtype=object).astype('c8')

correctly raises: Only the last processed array element could raise an error.

ahaldane added a commit to ahaldane/numpy that referenced this issue Sep 30, 2018

ahaldane added a commit to ahaldane/numpy that referenced this issue Oct 1, 2018

charris added a commit to charris/numpy that referenced this issue Oct 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment