{"record":{"id":"aeaa852628835335","repo":"pola-rs/polars","slug":"require-com-0-found-com-r","errorCode":null,"errorMessage":"require `com` >= 0 (found {com!r})","messagePattern":"require `com` >= 0 \\(found (.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/expr.py","lineNumber":12797,"sourceCode":"        return wrap_expr(result)\n\n\ndef _prepare_alpha(\n    com: float | int | None = None,\n    span: float | int | None = None,\n    half_life: float | int | None = None,\n    alpha: float | int | None = None,\n) -> float:\n    \"\"\"Normalise EWM decay specification in terms of smoothing factor 'alpha'.\"\"\"\n    if sum((param is not None) for param in (com, span, half_life, alpha)) > 1:\n        msg = (\n            \"parameters `com`, `span`, `half_life`, and `alpha` are mutually exclusive\"\n        )\n        raise ValueError(msg)\n    if com is not None:\n        if com < 0.0:\n            msg = f\"require `com` >= 0 (found {com!r})\"\n            raise ValueError(msg)\n        alpha = 1.0 / (1.0 + com)\n\n    elif span is not None:\n        if span < 1.0:\n            msg = f\"require `span` >= 1 (found {span!r})\"\n            raise ValueError(msg)\n        alpha = 2.0 / (span + 1.0)\n\n    elif half_life is not None:\n        if half_life <= 0.0:\n            msg = f\"require `half_life` > 0 (found {half_life!r})\"\n            raise ValueError(msg)\n        alpha = 1.0 - math.exp(-math.log(2.0) / half_life)\n\n    elif alpha is None:\n        msg = \"one of `com`, `span`, `half_life`, or `alpha` must be set\"\n        raise ValueError(msg)\n","sourceCodeStart":12779,"sourceCodeEnd":12815,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/expr.py#L12779-L12815","documentation":"_prepare_alpha validates that com (center of mass) is non-negative: com >= 0 maps to alpha = 1/(1+com), and negative com would yield alpha > 1, which is not a valid smoothing factor. The ValueError reports the offending value via {com!r} so you see exactly what was passed.","triggerScenarios":"pl.col('x').ewm_mean(com=-0.5); also float('nan') sneaking in from config compares False against 0.0... only strictly negative values trigger it.","commonSituations":"Sign errors in upstream calculations that derive com from data; user-tunable 'decay' knobs where a negative value was never rejected; porting formulas that define com differently (e.g. as a negative exponent).","solutions":["Pass com >= 0, or equivalently switch to alpha in (0, 1].","Trace where the negative value originates and clamp/validate it at the source (config parsing).","If you meant faster decay, use a smaller com (toward 0) — direction is opposite to what you may expect."],"exampleFix":"# before\npl.col('x').ewm_mean(com=-0.5)\n\n# after\npl.col('x').ewm_mean(com=0.5)","handlingStrategy":"validation","validationCode":"if com is not None:\n    assert com >= 0, f'com must be >= 0, got {com!r}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate numeric ranges at config load, not deep inside query construction.","Remember mapping: com=0 -> alpha=1 (no smoothing), larger com -> heavier smoothing."],"tags":["validation","polars","expression","ewm","statistics"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}