Skip to content

[BUG] Add int-array-like into random state #32511

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 3 commits 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: 1 addition & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ def makeMissingCustomDataframe(
Density : float, optional
Float in (0, 1) that gives the percentage of non-missing numbers in
the DataFrame.
random_state : {np.random.RandomState, int}, optional
random_state : {np.random.RandomState, int, integer array like}, optional
Random number generator or random seed.

See makeCustomDataframe for descriptions of the rest of the parameters.
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
is_bool_dtype,
is_extension_array_dtype,
is_integer,
is_list_like,
)
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
from pandas.core.dtypes.inference import _iterable_not_string
Expand Down Expand Up @@ -408,13 +409,16 @@ def random_state(state=None):
"""
if is_integer(state):
return np.random.RandomState(state)
elif is_list_like(state) and all(is_integer(item) for item in state):
return np.random.RandomState(state)
elif isinstance(state, np.random.RandomState):
return state
elif state is None:
return np.random
else:
raise ValueError(
"random_state must be an integer, a numpy RandomState, or None"
"random_state must be an integer, integer array like,"
"a numpy RandomState, or None"
)


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4691,7 +4691,7 @@ def sample(
If weights do not sum to 1, they will be normalized to sum to 1.
Missing values in the weights column will be treated as zero.
Infinite values not allowed.
random_state : int or numpy.random.RandomState, optional
random_state : int, integer array like or numpy.random.RandomState, optional
Seed for the random number generator (if int), or numpy RandomState
object.
axis : {0 or ‘index’, 1 or ‘columns’, None}, default None
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ def test_random_state():
state2 = npr.RandomState(10)
assert com.random_state(state2).uniform() == npr.RandomState(10).uniform()

# Check with array ike
state3 = com.random_state([1, 5, 10])
assert com.random_state(state3).uniform() == npr.RandomState([1, 5, 10]).uniform()

# check with no arg random state
assert com.random_state() is np.random

# Error for floats or strings
msg = "random_state must be an integer, a numpy RandomState, or None"
msg = (
"random_state must be an integer, integer array like,"
"a numpy RandomState, or None"
)
with pytest.raises(ValueError, match=msg):
com.random_state("test")

Expand Down