-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
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
Can't change dtype for non-continuous array #9496
Comments
Hmm, seems like there is no fundamental reason that could not work. |
Might take the view before slicing, if that is possible. |
Indeed. I suppose that strictly speaking, we only need the last dimension to be continuous.
The simplest option is to make a copy, e.g., If you need to reuse the same buffer, that should also be possible -- take a look at |
If possible, I would prefer not taking a copy, as the array is very large. Also I can't "take the view before slicing", because in reality I have a structured array with one of the fields being pairs of floats, which I need to convert to complex values. So, will try something along the lines of |
I don't think we can fix this without removing a deprecation from 2015-11-27 (1.11.0), which gives fortran-ordered arrays different and possibly conflicting semantics: In [11]: arr = np.arange(6.0).reshape((3, 2))
In [12]: arr
Out[12]:
array([[ 0., 1.],
[ 2., 3.],
[ 4., 5.]])
In [13]: arr.view(complex)
Out[13]:
array([[ 0.+1.j],
[ 2.+3.j],
[ 4.+5.j]])
In [14]: arr.T.view(complex)
C:\Users\wiese\Repos\numeric-python\numpy\runtests.py:1: DeprecationWarning: Changing the shape of non-C contiguous array by
descriptor assignment is deprecated. To maintain
the Fortran contiguity of a multidimensional Fortran
array, use 'a.T.view(...).T' instead
#!/usr/bin/env python
Out[14]: array([[ 0.+1.j, 2.+3.j, 4.+5.j]]) |
Actually, the semantics do not conflict, so I think this is solvable. Patch in the works |
In my initial PR to fix this (#9497, now closed), @jaimefrio made a comment against allowing it when the last axis is contiguous - which I'm repeating here to keep the discussion in one place:
|
My argument would be the semantics of This is already the semantics for C-contiguous arrays. It's not the semantics for F-contiguous, which is presumably why those semantics were deprecated in #6747. Relaxing the constraint to "last axis is contiguous" would also preserve this invariant. There was a pr that would also fix this at #5508. That PR does not preserve this invariant, instead have stride-dependant behaviour. Stride-dependant behaviour (beyond deciding whether to error) sounds like a bad idea to me, because few users are aware of the strides of their arrays . |
Yes, I was thinking the same thing. I agree that the behavior for view should not be stride-dependant. |
We've already defined the semantics of I think this is a restatement of what you're saying. |
Personally I don't see how supporting this case (leaving first axis strides as is) can be confusing at all - the same thing happens now with continuous arrays.
Could you please expand on this a bit? I'm not too familiar with these "inner" parts of numpy and can't get how to do this, even after looking at |
I take this remark back:
We can't fix this properly until we remove the deprecation. We're forced to either:
This code:
Is required to have Unfortunately, we gave a |
The conservative thing to do is to switch to a FutureWarning now and revisit this in a couple of years. |
@shoyer: How about doing the following before revisiting, listed by precedence:
This introduces as much new behavior as possible, without breaking the old code - and begins the next cycle for finally removing that code path, and completing the use of new behaviour |
Might want to take a look at #6614. The problems came in with relaxed strides, which allow arrays to be both Fortran and C contiguous. |
Wrong issue number? |
When I have a plain float array like
arr = np.ones((3, 4, 2))
and want to make acomplex
value from each two consequtivefloat
s it's easily possible:arr.view(complex)
gives the expected result. But when I want to perform the same with a non-continuous array, likearr[:, ::2, :].view(complex)
, it raisesValueError: new type not compatible with array.
. Clearly this conversion is possible without copies, but either I misunderstand something here or it's a bug innumpy
.Anyway, for this moment, are there any workarounds for this?
The text was updated successfully, but these errors were encountered: