Skip to content

Use np.random's RandomState when seed is None #13161

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ API changes
- 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`)
- Compat with ``np.round`` and timestamps (:issue:`12811`)
- An ``UnsupportedFunctionCall`` error is now raised if numpy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
- Calls to ``.sample()`` will respect random seed set via ``numpy.random.seed(n)`` (:issue:`13161`)


.. _whatsnew_0182.api.tolist:

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ def _random_state(state=None):
state : int, np.random.RandomState, None.
If receives an int, passes to np.random.RandomState() as seed.
If receives an np.random.RandomState object, just returns object.
If receives `None`, returns an np.random.RandomState object.
If receives `None`, returns np.random.
If receives anything else, raises an informative ValueError.
Default None.

Expand All @@ -2076,7 +2076,7 @@ def _random_state(state=None):
elif isinstance(state, np.random.RandomState):
return state
elif state is None:
return np.random.RandomState()
return np.random
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please update the docstring, too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Fixed.

On 05/17, Stephan Hoyer wrote:

@@ -2072,7 +2072,7 @@ def _random_state(state=None):
elif isinstance(state, np.random.RandomState):
return state
elif state is None:

  •    return np.random.RandomState()
    
  •    return np.random
    

can you please update the docstring, too?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
https://github.com/pydata/pandas/pull/13161/files/c8a38f99637ad44dad0db9118b17fd4e3c8643f3#r63603099

else:
raise ValueError("random_state must be an integer, a numpy "
"RandomState, or None")
2 changes: 1 addition & 1 deletion pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def test_random_state():
com._random_state(state2).uniform(), npr.RandomState(10).uniform())

# check with no arg random state
assert isinstance(com._random_state(), npr.RandomState)
assert com._random_state() is np.random

# Error for floats or strings
with tm.assertRaises(ValueError):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ def test_sample(self):
o.sample(frac=0.7, random_state=np.random.RandomState(test)),
o.sample(frac=0.7, random_state=np.random.RandomState(test)))

os1, os2 = [], []
for _ in range(2):
np.random.seed(test)
os1.append(o.sample(n=4))
os2.append(o.sample(frac=0.7))
self._compare(*os1)
self._compare(*os2)

# Check for error when random_state argument invalid.
with tm.assertRaises(ValueError):
o.sample(random_state='astring!')
Expand Down