{"record":{"id":"bfba474f00de5362","repo":"pola-rs/polars","slug":"cannot-specify-both-n-and-fraction-bfba47","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/list.py","lineNumber":253,"sourceCode":"        >>> df = pl.DataFrame({\"values\": [[1, 2, 3], [4, 5]], \"n\": [2, 1]})\n        >>> df.with_columns(\n        ...     sample=pl.col(\"values\").list.sample(\n        ...         n=pl.col(\"n\"), shuffle=False, seed=1\n        ...     )\n        ... )\n        shape: (2, 3)\n        ┌───────────┬─────┬───────────┐\n        │ values    ┆ n   ┆ sample    │\n        │ ---       ┆ --- ┆ ---       │\n        │ list[i64] ┆ i64 ┆ list[i64] │\n        ╞═══════════╪═════╪═══════════╡\n        │ [1, 2, 3] ┆ 2   ┆ [2, 3]    │\n        │ [4, 5]    ┆ 1   ┆ [5]       │\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.list_sample_fraction(\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.list_sample_n(n_pyexpr, with_replacement, shuffle, seed)\n        )\n\n    def sum(self) -> Expr:\n        \"\"\"","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/list.py#L235-L271","documentation":"The list namespace's Expr.list.sample mirrors the frame/expr sampler: `n` (items to draw per sub-list) and `fraction` (proportion per sub-list) are mutually exclusive and route to different Rust kernels (list_sample_n vs list_sample_fraction). Supplying both is ambiguous and rejected in the Python layer with ValueError.","triggerScenarios":"pl.col('vals').list.sample(n=2, fraction=0.5); also df.select(pl.col('l').list.sample(2, 0.5)) positional form.","commonSituations":"Sampling config with both knobs; adapting Expr.sample code to per-list sampling and keeping both arguments; pandas-derived habits where n and frac coexist.","solutions":["Keep one argument: list.sample(n=2) or list.sample(fraction=0.5).","Delete the redundant argument from shared config before forwarding."],"exampleFix":"# before\npl.col('vals').list.sample(n=2, fraction=0.5)\n\n# after\npl.col('vals').list.sample(n=2)","handlingStrategy":"validation","validationCode":"assert (n is None) != (fraction is None), 'list.sample: pass exactly one of n / fraction'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Same exclusivity rule as Expr.sample and DataFrame.sample — one sampling mode per call.","Store sampling config as ('n', value) or ('fraction', value), not two optional fields."],"tags":["validation","polars","expression","list","sampling"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}