Skip to content

Commit 595b0bc

Browse files
author
Allen Riddell
committed
Use np.random's RandomState when seed is None
The handle for numpy's current random state is ``np.random.mtrand._rand``. Rather than use the private API, return np.random, as the module makes available the same functions as an instance of RandomState. Compare https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/utils/validation.py#L573
1 parent 9d44e63 commit 595b0bc

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

doc/source/whatsnew/v0.18.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ API changes
5656
- Non-convertible dates in an excel date column will be returned without conversion and the column will be ``object`` dtype, rather than raising an exception (:issue:`10001`)
5757
- Compat with ``np.round`` and timestamps (:issue:`12811`)
5858
- An ``UnsupportedFunctionCall`` error is now raised if numpy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
59+
- Calls to ``.sample()`` will respect random seed set via ``numpy.random.seed(n)`` (:issue:`13161`)
60+
5961

6062
.. _whatsnew_0182.api.tolist:
6163

pandas/core/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ def _random_state(state=None):
20622062
state : int, np.random.RandomState, None.
20632063
If receives an int, passes to np.random.RandomState() as seed.
20642064
If receives an np.random.RandomState object, just returns object.
2065-
If receives `None`, returns an np.random.RandomState object.
2065+
If receives `None`, returns np.random.
20662066
If receives anything else, raises an informative ValueError.
20672067
Default None.
20682068
@@ -2076,7 +2076,7 @@ def _random_state(state=None):
20762076
elif isinstance(state, np.random.RandomState):
20772077
return state
20782078
elif state is None:
2079-
return np.random.RandomState()
2079+
return np.random
20802080
else:
20812081
raise ValueError("random_state must be an integer, a numpy "
20822082
"RandomState, or None")

pandas/tests/test_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def test_random_state():
695695
com._random_state(state2).uniform(), npr.RandomState(10).uniform())
696696

697697
# check with no arg random state
698-
assert isinstance(com._random_state(), npr.RandomState)
698+
assert com._random_state() is np.random
699699

700700
# Error for floats or strings
701701
with tm.assertRaises(ValueError):

pandas/tests/test_generic.py

+8
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ def test_sample(self):
415415
o.sample(frac=0.7, random_state=np.random.RandomState(test)),
416416
o.sample(frac=0.7, random_state=np.random.RandomState(test)))
417417

418+
os1, os2 = [], []
419+
for _ in range(2):
420+
np.random.seed(test)
421+
os1.append(o.sample(n=4))
422+
os2.append(o.sample(frac=0.7))
423+
self._compare(*os1)
424+
self._compare(*os2)
425+
418426
# Check for error when random_state argument invalid.
419427
with tm.assertRaises(ValueError):
420428
o.sample(random_state='astring!')

0 commit comments

Comments
 (0)