Discussion:
[Numpy-discussion] Deprecating silent truncation of floats when assigned to int array
Nathaniel Smith
2016-06-13 20:11:36 UTC
Permalink
It was recently pointed out:

https://github.com/numpy/numpy/issues/7730

that this code silently truncates floats:

In [1]: a = np.arange(10)

In [2]: a.dtype
Out[2]: dtype('int64')

In [3]: a[3] = 1.5

In [4]: a[3]
Out[4]: 1

The proposal is that we should deprecate this, and eventually turn it
into an error. Any objections?

We recently went through a similar deprecation cycle for in-place
operations, i.e., this used to silently truncate but now raises an
error:

In [1]: a = np.arange(10)

In [2]: a += 1.5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-9cf893a64492> in <module>()
----> 1 a += 1.5

TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('int64') with casting rule 'same_kind'

so the proposal here is to extend this to regular assignment.

-n
--
Nathaniel J. Smith -- https://vorpus.org
Ian Henriksen
2016-06-13 22:23:06 UTC
Permalink
Personally, I think this is a great idea. +1 to more informative errors.
Best,
Ian Henriksen
Post by Nathaniel Smith
https://github.com/numpy/numpy/issues/7730
In [1]: a = np.arange(10)
In [2]: a.dtype
Out[2]: dtype('int64')
In [3]: a[3] = 1.5
In [4]: a[3]
Out[4]: 1
The proposal is that we should deprecate this, and eventually turn it
into an error. Any objections?
We recently went through a similar deprecation cycle for in-place
operations, i.e., this used to silently truncate but now raises an
In [1]: a = np.arange(10)
In [2]: a += 1.5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-9cf893a64492> in <module>()
----> 1 a += 1.5
TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('int64') with casting rule 'same_kind'
so the proposal here is to extend this to regular assignment.
-n
--
Nathaniel J. Smith -- https://vorpus.org
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Ralf Gommers
2016-06-17 18:30:34 UTC
Permalink
On Tue, Jun 14, 2016 at 12:23 AM, Ian Henriksen <
Post by Ian Henriksen
Personally, I think this is a great idea. +1 to more informative errors.
+1 from me as well

Ralf
Post by Ian Henriksen
Best,
Ian Henriksen
Post by Nathaniel Smith
https://github.com/numpy/numpy/issues/7730
In [1]: a = np.arange(10)
In [2]: a.dtype
Out[2]: dtype('int64')
In [3]: a[3] = 1.5
In [4]: a[3]
Out[4]: 1
The proposal is that we should deprecate this, and eventually turn it
into an error. Any objections?
We recently went through a similar deprecation cycle for in-place
operations, i.e., this used to silently truncate but now raises an
In [1]: a = np.arange(10)
In [2]: a += 1.5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-9cf893a64492> in <module>()
----> 1 a += 1.5
TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('int64') with casting rule 'same_kind'
so the proposal here is to extend this to regular assignment.
-n
--
Nathaniel J. Smith -- https://vorpus.org
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...