{"record":{"id":"67b2a3f0da785424","repo":"pandas-dev/pandas","slug":"random-state-must-be-an-integer-array-like-a-bit","errorCode":null,"errorMessage":"random_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None","messagePattern":"random_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/common.py","lineNumber":475,"sourceCode":"        If receives anything else, raises an informative ValueError.\n\n        Default None.\n\n    Returns\n    -------\n    np.random.RandomState or np.random.Generator. If state is None, returns np.random\n\n    \"\"\"\n    if is_integer(state) or isinstance(state, (np.ndarray, np.random.BitGenerator)):\n        return np.random.RandomState(state)\n    elif isinstance(state, np.random.RandomState):\n        return state\n    elif isinstance(state, np.random.Generator):\n        return state\n    elif state is None:\n        return np.random  # type: ignore[return-value]\n    else:\n        raise ValueError(\n            \"random_state must be an integer, array-like, a BitGenerator, Generator, \"\n            \"a numpy RandomState, or None\"\n        )\n\n\n_T = TypeVar(\"_T\")  # Secondary TypeVar for use in pipe's type hints\n\n\n@overload\ndef pipe(\n    obj: _T,\n    func: Callable[Concatenate[_T, P], T],\n    *args: P.args,\n    **kwargs: P.kwargs,\n) -> T: ...\n\n\n@overload","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/common.py#L457-L493","documentation":"Raised by the random_state helper used by df.sample, Series.sample, DataFrame.sample, and other methods that accept a random_state argument. The helper accepts int, array-like, np.random.BitGenerator, np.random.RandomState, np.random.Generator, or None; anything else (string, float, tuple, custom object) raises ValueError listing the accepted types.","triggerScenarios":"df.sample(random_state='42'); df.sample(random_state=3.14); df.sample(random_state=(1,2)); passing a Python random.Random instance (not accepted — only NumPy RandomState/Generator).","commonSituations":"Passing a string seed from config without converting to int; mixing Python's random module with pandas; passing a float seed; passing a tuple/seed object from an ML framework.","solutions":["Pass an int seed: df.sample(random_state=42).","Pass an np.random.Generator: df.sample(random_state=np.random.default_rng(0)).","Convert types at the boundary: random_state=int(cfg['seed']) if cfg['seed'] else None.","Pass None for global NumPy RNG."],"exampleFix":"// before\ndf.sample(random_state='42')\n// after\nseed = int('42')\ndf.sample(random_state=seed)","handlingStrategy":"validation","validationCode":"import numpy as np\nacceptable = (int, np.integer, np.ndarray, np.random.BitGenerator, np.random.RandomState, np.random.Generator, type(None))\nif not isinstance(rs, acceptable):\n    raise ValueError(f'bad random_state: {rs!r}')\ndf.sample(random_state=rs)","typeGuard":"def is_valid_random_state(rs) -> bool:\n    import numpy as np\n    if rs is None:\n        return True\n    return isinstance(rs, (int, np.integer, np.ndarray, np.random.BitGenerator, np.random.RandomState, np.random.Generator))","tryCatchPattern":null,"preventionTips":["Coerce seed strings to int before passing to random_state.","Do not pass Python random.Random instances; use np.random.RandomState or Generator.","Validate the random_state type at config boundaries."],"tags":["value-error","random-state","sampling","api-misuse","type-validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}