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

MAINT: gracefully shuffle memoryviews #18327

Merged
merged 2 commits into from Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions numpy/random/_generator.pyx
Expand Up @@ -2,7 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
from collections.abc import MutableSequence
from collections.abc import Sequence

from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from cpython cimport (Py_INCREF, PyFloat_AsDouble)
Expand Down Expand Up @@ -4436,7 +4436,7 @@ cdef class Generator:
x[i] = buf
else:
# Untyped path.
if not isinstance(x, MutableSequence):
if not isinstance(x, Sequence):
# See gh-18206. We may decide to deprecate here in the future.
warnings.warn(
"`x` isn't a recognized object; `shuffle` is not guaranteed "
Expand Down
4 changes: 2 additions & 2 deletions numpy/random/mtrand.pyx
Expand Up @@ -2,7 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
from collections.abc import MutableSequence
from collections.abc import Sequence

import numpy as np

Expand Down Expand Up @@ -4473,7 +4473,7 @@ cdef class RandomState:
x[i] = buf
else:
# Untyped path.
if not isinstance(x, MutableSequence):
if not isinstance(x, Sequence):
# See gh-18206. We may decide to deprecate here in the future.
warnings.warn(
"`x` isn't a recognized object; `shuffle` is not guaranteed "
Expand Down
15 changes: 15 additions & 0 deletions numpy/random/tests/test_random.py
Expand Up @@ -510,6 +510,21 @@ def test_shuffle_masked(self):
assert_equal(
sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))

def test_shuffle_memoryview(self):
# gh-18273
# allow graceful handling of memoryviews
# (treat the same as arrays)
np.random.seed(self.seed)
a = np.arange(5).data
np.random.shuffle(a)
assert_equal(np.asarray(a), [0, 1, 4, 3, 2])
rng = np.random.RandomState(self.seed)
rng.shuffle(a)
assert_equal(np.asarray(a), [0, 1, 2, 3, 4])
rng = np.random.default_rng(self.seed)
rng.shuffle(a)
assert_equal(np.asarray(a), [4, 1, 0, 3, 2])

def test_beta(self):
np.random.seed(self.seed)
actual = np.random.beta(.1, .9, size=(3, 2))
Expand Down