
Hi, I noticed a type-checking inconsistency between assignments using slicing and fancy-indexing. The first will happily cast on assignment (regardless of type), while the second will throw a type error if there's reason to believe the casting will be unsafe. I'm not sure which would be the "correct" behavior, but the inconsistency is surprising. Best, -Tony Example:
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8)
# this runs without error
b[:5] = a[:5]
mask = a < 5 b[mask] = b[mask] TypeError: array cannot be safely cast to required type

On Sun, Oct 16, 2011 at 12:39 PM, Tony Yu <tsyu80@gmail.com> wrote:
Hi,
I noticed a type-checking inconsistency between assignments using slicing and fancy-indexing. The first will happily cast on assignment (regardless of type), while the second will throw a type error if there's reason to believe the casting will be unsafe. I'm not sure which would be the "correct" behavior, but the inconsistency is surprising.
Best, -Tony
Example:
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8)
# this runs without error
b[:5] = a[:5]
mask = a < 5 b[mask] = b[mask] TypeError: array cannot be safely cast to required type
And I just noticed that 1D arrays behave differently than 2D arrays. If you replace the above definitions of a, b with:
a = np.arange(10)[:, np.newaxis] b = np.ones((10, 1), dtype=np.uint8)
The rest of the code will run without error.

(16.10.2011 18:39), Tony Yu wrote:
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8)
# this runs without error
b[:5] = a[:5]
mask = a < 5 b[mask] = b[mask] TypeError: array cannot be safely cast to required type
Seems to be fixed in Git master
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8) mask = a < 5 b[mask] = b[mask] b[mask] = a[mask] np.__version__ '2.0.0.dev-1dc1877'
-- Pauli Virtanen

On Sun, Oct 16, 2011 at 12:49 PM, Pauli Virtanen <pav@iki.fi> wrote:
(16.10.2011 18:39), Tony Yu wrote:
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8)
# this runs without error
b[:5] = a[:5]
mask = a < 5 b[mask] = b[mask] TypeError: array cannot be safely cast to required type
Seems to be fixed in Git master
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8) mask = a < 5 b[mask] = b[mask] b[mask] = a[mask] np.__version__ '2.0.0.dev-1dc1877'
(I see you noticed the typo in my original example: b --> a). Agreed, I'm getting this error with an old master. I just tried master and it worked fine, but the maintenance branch ('1.6.2.dev-396dbb9') does still have this issue.

On Sun, Oct 16, 2011 at 11:02 AM, Tony Yu <tsyu80@gmail.com> wrote:
On Sun, Oct 16, 2011 at 12:49 PM, Pauli Virtanen <pav@iki.fi> wrote:
(16.10.2011 18:39), Tony Yu wrote:
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8)
# this runs without error
b[:5] = a[:5]
mask = a < 5 b[mask] = b[mask] TypeError: array cannot be safely cast to required type
Seems to be fixed in Git master
import numpy as np a = np.arange(10) b = np.ones(10, dtype=np.uint8) mask = a < 5 b[mask] = b[mask] b[mask] = a[mask] np.__version__ '2.0.0.dev-1dc1877'
(I see you noticed the typo in my original example: b --> a). Agreed, I'm getting this error with an old master. I just tried master and it worked fine, but the maintenance branch ('1.6.2.dev-396dbb9') does still have this issue.
1.6.2 hasn't been kept up to date. I suspect 1.7.0 will be the next release. Chuck
participants (3)
-
Charles R Harris
-
Pauli Virtanen
-
Tony Yu