summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRalf Gommers <ralf.gommers@gmail.com>2021-01-23 13:59:19 +0100
committerRalf Gommers <ralf.gommers@gmail.com>2021-01-23 13:59:19 +0100
commit164959a9049c05901dd4a2cc66c07a4df37527c1 (patch)
treeadea38d4ba456a03903d8d541796fc070a2e5d1d
parentbec2b07db5c999721d2d2c10334f3afe78aaadd9 (diff)
downloadnumpy-164959a9049c05901dd4a2cc66c07a4df37527c1.tar.gz
MAINT: warn when shuffling unrecognized objects
Closes gh-18206
-rw-r--r--numpy/random/_generator.pyx15
-rw-r--r--numpy/random/mtrand.pyx13
2 files changed, 23 insertions, 5 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index 7d652ce89..5a7d4a21a 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -2,6 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
+from collections.abc import MutableSequence
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from cpython cimport (Py_INCREF, PyFloat_AsDouble)
@@ -4347,14 +4348,14 @@ cdef class Generator:
"""
shuffle(x, axis=0)
- Modify a sequence in-place by shuffling its contents.
+ Modify an array or sequence in-place by shuffling its contents.
The order of sub-arrays is changed but their contents remains the same.
Parameters
----------
- x : array_like
- The array or list to be shuffled.
+ x : ndarray or MutableSequence
+ The array, list or mutable sequence to be shuffled.
axis : int, optional
The axis which `x` is shuffled along. Default is 0.
It is only supported on `ndarray` objects.
@@ -4428,6 +4429,14 @@ cdef class Generator:
x[i] = buf
else:
# Untyped path.
+ if not isinstance(x, (np.ndarray, MutableSequence)):
+ # See gh-18206. We may decide to deprecate here in the future.
+ warnings.warn(
+ "`x` isn't a recognized object; `shuffle` is not guaranteed "
+ "to behave correctly. E.g., non-numpy array/tensor objects "
+ "with view semantics may contain duplicates after shuffling."
+ )
+
if axis != 0:
raise NotImplementedError("Axis argument is only supported "
"on ndarray objects")
diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx
index d43e7f5aa..9ee3d9ff3 100644
--- a/numpy/random/mtrand.pyx
+++ b/numpy/random/mtrand.pyx
@@ -2,6 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
+from collections.abc import MutableSequence
import numpy as np
@@ -4402,8 +4403,8 @@ cdef class RandomState:
Parameters
----------
- x : array_like
- The array or list to be shuffled.
+ x : ndarray or MutableSequence
+ The array, list or mutable sequence to be shuffled.
Returns
-------
@@ -4468,6 +4469,14 @@ cdef class RandomState:
x[i] = buf
else:
# Untyped path.
+ if not isinstance(x, (np.ndarray, MutableSequence)):
+ # See gh-18206. We may decide to deprecate here in the future.
+ warnings.warn(
+ "`x` isn't a recognized object; `shuffle` is not guaranteed "
+ "to behave correctly. E.g., non-numpy array/tensor objects "
+ "with view semantics may contain duplicates after shuffling."
+ )
+
with self.lock:
for i in reversed(range(1, n)):
j = random_interval(&self._bitgen, i)