Skip to content

TYP: RandomState alias #42171

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

Merged
merged 1 commit into from
Jun 22, 2021
Merged
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
1 change: 1 addition & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from pandas._typing import (
AnyArrayLike,
NpDtype,
RandomState,
Scalar,
T,
)
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
Level,
Manager,
NpDtype,
RandomState,
Renamer,
StorageOptions,
T,
Expand Down Expand Up @@ -5146,7 +5147,7 @@ def sample(
frac=None,
replace=False,
weights=None,
random_state=None,
random_state: RandomState | None = None,
axis=None,
) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class providing the base-class of operations.
FrameOrSeries,
FrameOrSeriesUnion,
IndexLabel,
RandomState,
Scalar,
T,
final,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down