Skip to content

Conversation

ev-br
Copy link
Contributor

@ev-br ev-br commented Jun 7, 2025

Otherwise, round returns a view for integer arguments and a copy otherwise. All other "rounding" functions (ceil, floor, trunc, rint), always return copies. Thus, make round consistent with the rest of them.

fixes #29124

@charris
Copy link
Member

charris commented Jun 7, 2025

Needs a release note.

@ev-br
Copy link
Contributor Author

ev-br commented Jun 7, 2025

Release note snippet added.

@rgommers rgommers added this to the 2.4.0 release milestone Jun 8, 2025
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems nobody had an opinion about this besides mild agreement, so let's get this in.
If you are excited, about test for that order fix would be nice. We don't tend to strictly guarantee it, but it's the better thing when it's easy.

@@ -637,8 +637,7 @@ PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
return (PyObject *)out;
}
else {
Py_INCREF(a);
return (PyObject *)a;
return PyArray_Copy(a);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return PyArray_Copy(a);
return PyArray_NewCopy(a, NPY_KEEPORDER);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... This made the test fail, but I hope that needs a test adjustment only :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I parroted a PyArray_Copy from the complex dtype code path of this same function, so if we insist on NPY_KEEPORDER for real dtypes, we need to follow for complex dtypes, too. Done now.

ev-br added 3 commits August 20, 2025 20:56
Otherwise, `round` returns a view for integer arguments and a copy otherwise.
All other "rounding" functions (ceil, floor, trunc, rint), always return copies.
Thus, make `round` consistent with them.
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @ev-br, sorry for taking so long and thanks for the small follow-ups! Let's give this a shot :).

@seberg seberg merged commit 71eebaf into numpy:main Aug 22, 2025
77 checks passed
bwhitt7 pushed a commit to bwhitt7/numpy that referenced this pull request Aug 24, 2025
* BUG: make round consistently return a copy

Otherwise, `round` returns a view for integer arguments and a copy otherwise.
All other "rounding" functions (ceil, floor, trunc, rint), always return copies.
Thus, make `round` consistent with them.

* DOC: add a release note snippet for the `round` change

* TST: add a test for round preserving the order

* MAINT: address review comments

* BUG: round: NPY_KEEPORDER for complex arrays, too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: For integer arguments, round returns a view while ceil returns a copy
4 participants