diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-10-10 23:29:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-10 23:29:06 -0700 |
commit | e64699dcaca9eb0dd97deabae01ffc2884cacbb0 (patch) | |
tree | 3eb305b2a44907df99385704cf6375901d564156 | |
parent | bb5d666e84e2eb294543a67c6143d7e9124d1c73 (diff) | |
parent | 6fbbadb676dfa7cee56be3bd17b92bde661f8869 (diff) | |
download | numpy-e64699dcaca9eb0dd97deabae01ffc2884cacbb0.tar.gz |
Merge pull request #9718 from hemildesai/deprecate_truth_testing_on_empty_arrays
DEP: Deprecate truth testing on empty arrays
-rw-r--r-- | doc/release/1.14.0-notes.rst | 2 | ||||
-rw-r--r-- | numpy/core/numeric.py | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/number.c | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 13 |
4 files changed, 22 insertions, 1 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst index a4b61e038..20097e97e 100644 --- a/doc/release/1.14.0-notes.rst +++ b/doc/release/1.14.0-notes.rst @@ -21,6 +21,8 @@ New functions Deprecations ============ +* Truth testing on an empty array is deprecated. To check if an array is not + empty, use ``array.size > 0``. Future Changes ============== diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index ce17a1900..5b10361fe 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -406,7 +406,7 @@ def count_nonzero(a, axis=None): array([2, 3]) """ - if axis is None or axis == (): + if axis is None or (isinstance(axis, tuple) and axis == ()): return multiarray.count_nonzero(a) a = asanyarray(a) diff --git a/numpy/core/src/multiarray/number.c b/numpy/core/src/multiarray/number.c index 08d177992..dbf71230a 100644 --- a/numpy/core/src/multiarray/number.c +++ b/numpy/core/src/multiarray/number.c @@ -797,6 +797,12 @@ _array_nonzero(PyArrayObject *mp) return res; } else if (n == 0) { + /* 2017-09-25, 1.14 */ + if (DEPRECATE("The truth value of an empty array is ambiguous. " + "Returning False, but in future this will result in an error. " + "Use `array.size > 0` to check that an array is not empty.") < 0) { + return -1; + } return 0; } else { diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index e3e8c32f9..d7c402b53 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -450,5 +450,18 @@ class TestDatetimeEvent(_DeprecationTestCase): self.assert_deprecated(cls, args=(1, ('ms', 2, 1, 63))) +class TestTruthTestingEmptyArrays(_DeprecationTestCase): + # 2017-09-25, 1.14.0 + message = '.*truth value of an empty array is ambiguous.*' + + def test_1d(self): + self.assert_deprecated(bool, args=(np.array([]),)) + + def test_2d(self): + self.assert_deprecated(bool, args=(np.zeros((1, 0)),)) + self.assert_deprecated(bool, args=(np.zeros((0, 1)),)) + self.assert_deprecated(bool, args=(np.zeros((0, 0)),)) + + if __name__ == "__main__": run_module_suite() |