-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: np.asarray does not create new array view when different but compatible type code is passed in the dtype argument (Trac #870) #1468
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
Comments
trac user tihocan wrote on 2010-01-20 Another workaround
|
trac user tihocan wrote on 2010-01-21 Replying to [comment:2 tihocan]:
Sorry, the code above is not a good idea, you need to give a dtype to asarray as well in case a conversion is needed. Correct version:
|
@mwiebe wrote on 2011-03-23 This is yet another issue that would go away by switching to implementing based on the sized types instead of the C types. |
Milestone changed to |
@mwiebe Was the implementation ultimately fixed? |
PR is in preparation. Intended behavior:
|
For `asarray` and for the `dtype` equality operator, equivalent dtype aliases are considered exact matches. Note: Intended behavior of `np.dtype('i') == np.dtype('l')` is to test compatibility, not identity. This change does not affect the behavior of `PyArray_EquivTypes()`, and the `__eq__` operator for `dtype` continues to map to `PyArray_EquivTypes()`. Fixes numpy#1468.
For `asarray` and for the `dtype` equality operator, equivalent dtype aliases are considered exact matches. Note: Intended behavior of `np.dtype('i') == np.dtype('l')` is to test compatibility, not identity. This change does not affect the behavior of `PyArray_EquivTypes()`, and the `__eq__` operator for `dtype` continues to map to `PyArray_EquivTypes()`. Fixes numpy#1468.
For `asarray` and for the `dtype` equality operator, equivalent dtype aliases were considered exact matches. This change ensures that the returned array has a descriptor that exactly matches the requested dtype. Note: Intended behavior of `np.dtype('i') == np.dtype('l')` is to test compatibility, not identity. This change does not affect the behavior of `PyArray_EquivTypes()`, and the `__eq__` operator for `dtype` continues to map to `PyArray_EquivTypes()`. Fixes numpy#1468.
Original ticket http://projects.scipy.org/numpy/ticket/870 on 2008-08-01 by trac user damian.eads, assigned to @stefanv.
Examining
ndarray
type codes is common in C extensions before passing array contents to functions expectingint*
orlong*
. The type codes are ideal ensure the contents can be safely casted toint*
orlong*
on the host machine. In some cases, theint
andlong
types are of the same size but their type codes are different.np.asarray(..., dtype='i')
should be used to create a new array view with the type code the C type code expected by the C extension; however, it does not appear to work when using it for this purpose.Problem
I construct X as a C-long array and then I cast it to a C-int array Y (using
np.asarray
) but the type code does not change. However, when constructing the array from scratch as a C-int (usingnp.zeros
), I get the right type code (ie 5).Expectation of Behavior
I assumed that when X gets casted to a C-int, no copying should occur but a new array view should be constructed with the C-int type code.
Workaround
Casting from a C-long (type code 7) to a double to a C-int returns an array with the right type code but an ugly double copy occurs.
Recreation
Stefan van der Walt observes that the
dtype
objects are compared for equality but not the type codes. This explains why theasarray
function does not create a new view.Fix
Check the type codes for equality, not the
dtype
objects, to decide whether to create a new array view innp.asarray
.summary @seberg:
This is still true, the array fast-paths to do the no-copy behaviour is correct to not copy, but apparently not aggressive enough with creating views, on 64bit linux:
The good news: This really should be a simple fix!
The text was updated successfully, but these errors were encountered: