Skip to content

Commit 3f70894

Browse files
mzeitlin11JulianWgs
authored andcommitted
TYP: RandomState alias (pandas-dev#42171)
1 parent eb6753a commit 3f70894

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

pandas/_typing.py

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
123123
Frequency = Union[str, "DateOffset"]
124124
Axes = Collection[Any]
125+
RandomState = Union[int, ArrayLike, np.random.Generator, np.random.RandomState]
125126

126127
# dtypes
127128
NpDtype = Union[str, np.dtype]

pandas/core/common.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas._typing import (
3131
AnyArrayLike,
3232
NpDtype,
33+
RandomState,
3334
Scalar,
3435
T,
3536
)
@@ -388,13 +389,13 @@ def standardize_mapping(into):
388389
return into
389390

390391

391-
def random_state(state=None):
392+
def random_state(state: RandomState | None = None) -> np.random.RandomState:
392393
"""
393394
Helper function for processing random_state arguments.
394395
395396
Parameters
396397
----------
397-
state : int, array-like, BitGenerator (NumPy>=1.17), np.random.RandomState, None.
398+
state : int, array-like, BitGenerator, np.random.RandomState, None.
398399
If receives an int, array-like, or BitGenerator, passes to
399400
np.random.RandomState() as seed.
400401
If receives an np.random.RandomState object, just returns object.
@@ -403,8 +404,8 @@ def random_state(state=None):
403404
404405
.. versionchanged:: 1.1.0
405406
406-
array-like and BitGenerator (for NumPy>=1.18) object now passed to
407-
np.random.RandomState() as seed
407+
array-like and BitGenerator object now passed to np.random.RandomState()
408+
as seed
408409
409410
Default None.
410411

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
Level,
4848
Manager,
4949
NpDtype,
50+
RandomState,
5051
Renamer,
5152
StorageOptions,
5253
T,
@@ -5146,7 +5147,7 @@ def sample(
51465147
frac=None,
51475148
replace=False,
51485149
weights=None,
5149-
random_state=None,
5150+
random_state: RandomState | None = None,
51505151
axis=None,
51515152
) -> FrameOrSeries:
51525153
"""
@@ -5177,8 +5178,7 @@ def sample(
51775178
Missing values in the weights column will be treated as zero.
51785179
Infinite values not allowed.
51795180
random_state : int, array-like, BitGenerator, np.random.RandomState, optional
5180-
If int, array-like, or BitGenerator (NumPy>=1.17), seed for
5181-
random number generator
5181+
If int, array-like, or BitGenerator, seed for random number generator.
51825182
If np.random.RandomState, use as numpy RandomState object.
51835183
51845184
.. versionchanged:: 1.1.0

pandas/core/groupby/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class providing the base-class of operations.
4747
FrameOrSeries,
4848
FrameOrSeriesUnion,
4949
IndexLabel,
50+
RandomState,
5051
Scalar,
5152
T,
5253
final,
@@ -3189,7 +3190,7 @@ def sample(
31893190
frac: float | None = None,
31903191
replace: bool = False,
31913192
weights: Sequence | Series | None = None,
3192-
random_state=None,
3193+
random_state: RandomState | None = None,
31933194
):
31943195
"""
31953196
Return a random sample of items from each group.
@@ -3216,8 +3217,7 @@ def sample(
32163217
Values must be non-negative with at least one positive element
32173218
within each group.
32183219
random_state : int, array-like, BitGenerator, np.random.RandomState, optional
3219-
If int, array-like, or BitGenerator (NumPy>=1.17), seed for
3220-
random number generator
3220+
If int, array-like, or BitGenerator, seed for random number generator.
32213221
If np.random.RandomState, use as numpy RandomState object.
32223222
32233223
Returns

0 commit comments

Comments
 (0)