From 76b2cc27e5919abcf64ff245bb02bf6210b10073 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Mon, 21 Jun 2021 14:12:42 -0400 Subject: [PATCH] TYP: RandomState alias --- pandas/_typing.py | 1 + pandas/core/common.py | 9 +++++---- pandas/core/generic.py | 6 +++--- pandas/core/groupby/groupby.py | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 12d23786c3387..a9852dd4b13cf 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -122,6 +122,7 @@ JSONSerializable = Optional[Union[PythonScalar, List, Dict]] Frequency = Union[str, "DateOffset"] Axes = Collection[Any] +RandomState = Union[int, ArrayLike, np.random.Generator, np.random.RandomState] # dtypes NpDtype = Union[str, np.dtype] diff --git a/pandas/core/common.py b/pandas/core/common.py index 183607ebb489d..fb4c4910a8a95 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -30,6 +30,7 @@ from pandas._typing import ( AnyArrayLike, NpDtype, + RandomState, Scalar, T, ) @@ -388,13 +389,13 @@ def standardize_mapping(into): return into -def random_state(state=None): +def random_state(state: RandomState | None = None) -> np.random.RandomState: """ Helper function for processing random_state arguments. Parameters ---------- - state : int, array-like, BitGenerator (NumPy>=1.17), np.random.RandomState, None. + state : int, array-like, BitGenerator, np.random.RandomState, None. If receives an int, array-like, or BitGenerator, passes to np.random.RandomState() as seed. If receives an np.random.RandomState object, just returns object. @@ -403,8 +404,8 @@ def random_state(state=None): .. versionchanged:: 1.1.0 - array-like and BitGenerator (for NumPy>=1.18) object now passed to - np.random.RandomState() as seed + array-like and BitGenerator object now passed to np.random.RandomState() + as seed Default None. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5bd845534fc96..30c33a951e145 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -47,6 +47,7 @@ Level, Manager, NpDtype, + RandomState, Renamer, StorageOptions, T, @@ -5146,7 +5147,7 @@ def sample( frac=None, replace=False, weights=None, - random_state=None, + random_state: RandomState | None = None, axis=None, ) -> FrameOrSeries: """ @@ -5177,8 +5178,7 @@ def sample( Missing values in the weights column will be treated as zero. Infinite values not allowed. random_state : int, array-like, BitGenerator, np.random.RandomState, optional - If int, array-like, or BitGenerator (NumPy>=1.17), seed for - random number generator + If int, array-like, or BitGenerator, seed for random number generator. If np.random.RandomState, use as numpy RandomState object. .. versionchanged:: 1.1.0 diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 0080791a51a4b..7fd5f2d52d23c 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -47,6 +47,7 @@ class providing the base-class of operations. FrameOrSeries, FrameOrSeriesUnion, IndexLabel, + RandomState, Scalar, T, final, @@ -3184,7 +3185,7 @@ def sample( frac: float | None = None, replace: bool = False, weights: Sequence | Series | None = None, - random_state=None, + random_state: RandomState | None = None, ): """ Return a random sample of items from each group. @@ -3211,8 +3212,7 @@ def sample( Values must be non-negative with at least one positive element within each group. random_state : int, array-like, BitGenerator, np.random.RandomState, optional - If int, array-like, or BitGenerator (NumPy>=1.17), seed for - random number generator + If int, array-like, or BitGenerator, seed for random number generator. If np.random.RandomState, use as numpy RandomState object. Returns