{"record":{"id":"5d7a7b2a014bd7bf","repo":"pola-rs/polars","slug":"cannot-specify-both-n-and-fraction-5d7a7b","errorCode":null,"errorMessage":"cannot specify both `n` and `fraction`","messagePattern":"cannot specify both `n` and `fraction`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/expr.py","lineNumber":11034,"sourceCode":"        >>> df.select(\n        ...     pl.col(\"a\").sample(\n        ...         fraction=1.0, with_replacement=True, shuffle=False, seed=1\n        ...     )\n        ... )\n        shape: (3, 1)\n        ┌─────┐\n        │ a   │\n        │ --- │\n        │ i64 │\n        ╞═════╡\n        │ 3   │\n        │ 3   │\n        │ 1   │\n        └─────┘\n        \"\"\"\n        if n is not None and fraction is not None:\n            msg = \"cannot specify both `n` and `fraction`\"\n            raise ValueError(msg)\n\n        if fraction is not None:\n            fraction_pyexpr = parse_into_expression(fraction)\n            return wrap_expr(\n                self._pyexpr.sample_frac(\n                    fraction_pyexpr, with_replacement, shuffle, seed\n                )\n            )\n\n        if n is None:\n            n = 1\n        n_pyexpr = parse_into_expression(n)\n        return wrap_expr(\n            self._pyexpr.sample_n(n_pyexpr, with_replacement, shuffle, seed)\n        )\n\n    @deprecate_renamed_parameter(\"min_periods\", \"min_samples\", version=\"1.21.0\")\n    def ewm_mean(","sourceCodeStart":11016,"sourceCodeEnd":11052,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/expr.py#L11016-L11052","documentation":"Expr.sample rejects sampling by count and by proportion at the same time: `n` (absolute number of rows) and `fraction` (0..1 proportion) are mutually exclusive and dispatch to different Rust kernels (sample_n vs sample_frac). Passing both is ambiguous, so the Python layer raises ValueError immediately.","triggerScenarios":"pl.col('a').sample(n=10, fraction=0.5), or sample(n, frac) style ported from pandas where both were accepted.","commonSituations":"Porting pandas df.sample(n=..., frac=...) code — pandas allows frac plus n as an upper bound, polars does not; config-driven sampling where both knobs exist and someone enables both.","solutions":["Pick one: sample(n=10) for a fixed count or sample(fraction=0.5) for a proportion.","If you ported from pandas, translate frac= to fraction= and delete n.","To cap a proportional sample, compute the desired count yourself and pass only n."],"exampleFix":"# before\npl.col('a').sample(n=10, fraction=0.5, with_replacement=True)\n\n# after\npl.col('a').sample(n=10, with_replacement=True)","handlingStrategy":"validation","validationCode":"assert (n is None) != (fraction is None), 'sample: pass exactly one of n / fraction'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When porting pandas code, remember pandas frac has no direct two-argument equivalent in polars.","Model sampling config as a single discriminated choice (either count or fraction), never two optional fields."],"tags":["validation","polars","expression","sampling"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}