[Numpy-discussion] Deprecating silent truncation of floats when assigned to int array

Nathaniel Smith njs at pobox.com
Mon Jun 13 16:11:36 EDT 2016


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



More information about the NumPy-Discussion mailing list