Skip to content
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

Deprecate/remove silent truncation on assignment of floats into an integer array #7730

Open
markrichardson opened this issue Jun 11, 2016 · 11 comments

Comments

@markrichardson
Copy link

Ubuntu 14.04
Python 2.7.11

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.10.4'

In [3]: v = 4 - 0.1

In [4]: np.array([v])
Out[4]: array([ 3.9])

In [5]: a = np.array([1]); a[0] = v; a
Out[5]: array([3])
@seberg
Copy link
Member

seberg commented Jun 11, 2016

The behaviour is of course correct, a is autotyped to be an integer array, so the float is cast to an integer on assignment. This could have a warning in principle for the unsafe cast, but it is expected and has to work that way since numpy arrays are (normally) typed.

@seberg seberg closed this as completed Jun 11, 2016
@charris
Copy link
Member

charris commented Jun 11, 2016

Note that numpy doesn't round in these cases, it truncates. @seberg I thought this was deprecated, but it doesn't seem so.

@seberg
Copy link
Member

seberg commented Jun 11, 2016

There is only a deprecation for the in place ufuncs, maybe we should think about making this deprecate too?

@charris
Copy link
Member

charris commented Jun 11, 2016

@seberg Sounds like a good idea, we should raise it on the list.

@njsmith
Copy link
Member

njsmith commented Jun 13, 2016

@njsmith njsmith reopened this Jun 13, 2016
@njsmith njsmith changed the title Erroneous rounding upon assignment to array element Deprecate/remove silent truncation on assignment of floats into an integer array Jun 17, 2016
@njsmith
Copy link
Member

njsmith commented Jun 17, 2016

Sounds like everyone is in favor, so I renamed the bug report. I guess concretely the desired change should follow the same path we used for in-place operations? Which IIRC was that we switched it to same_kind casting, with a deprecation period.

@seberg
Copy link
Member

seberg commented Jun 18, 2016

Yeah, would have to be a pretty long deprecation just like there. I am a little worried that the workaround with copyto or casting the right hand side is sometimes a bit annoying. But I suppose that it should be rare that a bit of explicit casting/more convolved stuff does not hurt and the error can easily tell you the solution.

@stsievert
Copy link

I think this is an important usability issue. One of my labmates @sumeetsk wasted a month of research effort on this bug (and just before his defense too). It boiled down to

>>> a = np.array([0, 1, 2])
>>> a[0] = 1.1
>>> print(a)
... array([1, 1, 2])

which is unexpected. This issue is also mentioned in a textbook:

if you attempt to insert a floating-point value to an integer array, the value will be silently truncated. Don't be caught unaware by this behavior!

@jakevdp's Data Science Handbook, Chapter 2.2 "The Basics of NumPy Arrays"

I'll try to submit a PR for this issue. This is still around in NumPy 1.14.3, and the recent release notes don't seem to mention it.

@sumeetsk
Copy link

sumeetsk commented Dec 5, 2018

What makes this especially unexpected / counter-intuitive is that python does not behave this way.

image

@mattip
Copy link
Member

mattip commented Dec 23, 2018

Overflow and truncation seem to be mentioned often as issues. Maybe we could have a verbose mode that would report overflow and truncation as warnings, at a cost in performance

@stevengj
Copy link

(Note that this issue and #8733 are basically duplicates.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants