|
15 | 15 |
|
16 | 16 | from pandas._libs import lib, tslibs
|
17 | 17 | from pandas._typing import T
|
| 18 | +from pandas.compat.numpy import _np_version_under1p17 |
18 | 19 |
|
19 | 20 | from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
|
20 | 21 | from pandas.core.dtypes.common import (
|
@@ -392,26 +393,41 @@ def random_state(state=None):
|
392 | 393 |
|
393 | 394 | Parameters
|
394 | 395 | ----------
|
395 |
| - state : int, np.random.RandomState, None. |
396 |
| - If receives an int, passes to np.random.RandomState() as seed. |
| 396 | + state : int, array-like, BitGenerator (NumPy>=1.17), np.random.RandomState, None. |
| 397 | + If receives an int, array-like, or BitGenerator, passes to |
| 398 | + np.random.RandomState() as seed. |
397 | 399 | If receives an np.random.RandomState object, just returns object.
|
398 | 400 | If receives `None`, returns np.random.
|
399 | 401 | If receives anything else, raises an informative ValueError.
|
| 402 | +
|
| 403 | + ..versionchanged:: 1.1.0 |
| 404 | +
|
| 405 | + array-like and BitGenerator (for NumPy>=1.17) object now passed to |
| 406 | + np.random.RandomState() as seed |
| 407 | +
|
400 | 408 | Default None.
|
401 | 409 |
|
402 | 410 | Returns
|
403 | 411 | -------
|
404 | 412 | np.random.RandomState
|
| 413 | +
|
405 | 414 | """
|
406 |
| - if is_integer(state): |
| 415 | + if ( |
| 416 | + is_integer(state) |
| 417 | + or is_array_like(state) |
| 418 | + or (not _np_version_under1p17 and isinstance(state, np.random.BitGenerator)) |
| 419 | + ): |
407 | 420 | return np.random.RandomState(state)
|
408 | 421 | elif isinstance(state, np.random.RandomState):
|
409 | 422 | return state
|
410 | 423 | elif state is None:
|
411 | 424 | return np.random
|
412 | 425 | else:
|
413 | 426 | raise ValueError(
|
414 |
| - "random_state must be an integer, a numpy RandomState, or None" |
| 427 | + ( |
| 428 | + "random_state must be an integer, array-like, a BitGenerator, " |
| 429 | + "a numpy RandomState, or None" |
| 430 | + ) |
415 | 431 | )
|
416 | 432 |
|
417 | 433 |
|
|
0 commit comments