10000 DEP: Deprecate nonzero(0d) in favor of calling atleast_1d explicitly by eric-wieser · Pull Request #13708 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DEP: Deprecate nonzero(0d) in favor of calling atleast_1d explicitly #13708

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

Merged
merged 2 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
DEP: Deprecate nonzero(0d) in favor of calling atleast_1d explicitly
This is a weird enough corner case that either:
* No one is running into it, and downstream code is broken on 0d arrays anyway
* People already have hacks in place to work around it:
  * Calling `nonzero` but handling the result specially if the array was 0d. We want to dissuade this in case we change the result of nonzero to be better in future.
  * Not calling `nonzero` at all if the array is zerod (such as by using `atleast1d`). These users are unaffected.
  • Loading branch information
eric-wieser committed Jun 5, 2019
commit f427b610b2f63dd43d9a2584944fa3265249e694
7 changes: 7 additions & 0 deletions doc/release/1.17.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ from python.
This deprecation should not affect many users since arrays created in such
a manner are very rare in practice and only available through the NumPy C-API.

`numpy.nonzero` should no longer be called on 0d arrays
-------------------------------------------------------
The behavior of nonzero on 0d arrays was surprising, making uses of it almost
always incorrect. If the old behavior was intended, it can be preserved without
a warning by using ``nonzero(atleast_1d(arr))`` instead of ``nonzero(arr)``.
In a future release, it is most likely this will raise a `ValueError`.


Future Changes
==============
Expand Down
20 changes: 19 additions & 1 deletion numpy/core/src/multiarray/item_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2208,8 +2208,26 @@ PyArray_Nonzero(PyArrayObject *self)
NpyIter_GetMultiIndexFunc *get_multi_index;
char **dataptr;

/* Special case - nonzero(zero_d) is nonzero(atleast1d(zero_d)) */
/* Special case - nonzero(zero_d) is nonzero(atleast_1d(zero_d)) */
if (ndim == 0) {
char const* msg;
if (PyArray_ISBOOL(self)) {
msg =
"Calling nonzero on 0d arrays is deprecated, as it behaves "
"surprisingly. Use `atleast_1d(cond).nonzero()` if the old "
"behavior was intended. If the context of this warning is of "
"the form `arr[nonzero(cond)]`, just use `arr[cond]`.";
}
else {
msg =
"Calling nonzero on 0d arrays is deprecated, as it behaves "
"surprisingly. Use `atleast_1d(arr).nonzero()` if the old "
"behavior was intended.";
}
if (DEPRECATE(msg) < 0) {
return NULL;
}

static npy_intp const zero_dim_shape[1] = {1};
static npy_intp const zero_dim_strides[1] = {0};

Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,10 @@ class TestShape1Fields(_DeprecationTestCase):
# 2019-05-20, 1.17.0
def test_shape_1_fields(self):
self.assert_deprecated(np.dtype, args=([('a', int, 1)],))


class TestNonZero(_DeprecationTestCase):
# 2019-05-26, 1.17.0
def test_zerod(self):
self.assert_deprecated(lambda: np.nonzero(np.array(0)))
self.assert_deprecated(lambda: np.nonzero(np.array(1)))
16 changes: 14 additions & 2 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,12 +976,24 @@ def test_nonzero_trivial(self):
assert_equal(np.count_nonzero(np.array([], dtype='?')), 0)
assert_equal(np.nonzero(np.array([])), ([],))

assert_equal(np.count_nonzero(np.array([0])), 0)
assert_equal(np.count_nonzero(np.array([0], dtype='?')), 0)
assert_equal(np.nonzero(np.array([0])), ([],))

assert_equal(np.count_nonzero(np.array([1])), 1)
assert_equal(np.count_nonzero(np.array([1], dtype='?')), 1)
assert_equal(np.nonzero(np.array([1])), ([0],))

def test_nonzero_zerod(self):
assert_equal(np.count_nonzero(np.array(0)), 0)
assert_equal(np.count_nonzero(np.array(0, dtype='?')), 0)
assert_equal(np.nonzero(np.array(0)), ([],))
with assert_warns(DeprecationWarning):
assert_equal(np.nonzero(np.array(0)), ([],))

assert_equal(np.count_nonzero(np.array(1)), 1)
assert_equal(np.count_nonzero(np.array(1, dtype='?')), 1)
assert_equal(np.nonzero(np.array(1)), ([0],))
with assert_warns(DeprecationWarning):
assert_equal(np.nonzero(np.array(1)), ([0],))

def test_nonzero_onedim(self):
x = np.array([1, 0, 2, -1, 0, 0, 8])
Expand Down
0