{"record":{"id":"bc97c6b1e7eeb4d4","repo":"pola-rs/polars","slug":"series-length-mismatch-expected-n-r-found-m-r","errorCode":null,"errorMessage":"Series length mismatch: expected {n!r}, found {m!r}","messagePattern":"Series length mismatch: expected (.+?), found (.+?)","errorType":"exception","errorClass":"ShapeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":5925,"sourceCode":"\n        Examples\n        --------\n        >>> s = pl.Series(\"a\", [1, 2, 3])\n        >>> s2 = pl.Series(\"b\", [4.0, 5.0, 6.0])\n        >>> s.dot(s2)\n        32.0\n\n        Parameters\n        ----------\n        other\n            Series (or array) to compute dot product with.\n        \"\"\"\n        if not isinstance(other, Series):\n            other = Series(other)\n        if len(self) != len(other):\n            n, m = len(self), len(other)\n            msg = f\"Series length mismatch: expected {n!r}, found {m!r}\"\n            raise ShapeError(msg)\n        return self._s.dot(other._s)\n\n    def mode(self, *, maintain_order: bool = False) -> Series:\n        \"\"\"\n        Compute the most occurring value(s).\n\n        Can return multiple Values.\n\n        Parameters\n        ----------\n        maintain_order\n            Maintain order of data. This requires more work.\n\n        Examples\n        --------\n        >>> s = pl.Series(\"a\", [1, 2, 2, 3])\n        >>> s.mode()\n        shape: (1,)","sourceCodeStart":5907,"sourceCodeEnd":5943,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L5907-L5943","documentation":"Raised when a binary Series operation (here `Series.dot`, and the same message is reused in DataFrame construction helpers) receives a Series/sequence whose length differs from the receiver's. Polars requires positional alignment between operands, so mismatched lengths cannot be evaluated element-wise.","triggerScenarios":"Calling s.dot(other) where len(other) != len(self); passing dict/list columns of unequal lengths to DataFrame(...) via dict_to_pydf or _handle_columns_arg; supplying nested sequences whose inner lengths disagree.","commonSituations":"Computing a dot product against a weight vector of the wrong size; building a DataFrame from dict values where one list has extra/missing rows after a filtered slice; off-by-one after dropping rows from one column only.","solutions":["Check len(a) == len(b) before the operation and trim or pad the shorter operand.","Re-derive the second Series from the same filtered/sliced source so both share indices.","If an intentional broadcast is meant, use pl.lit(value) / Expr broadcasting instead of Series-to-Series."],"exampleFix":"# before\nweights = pl.Series([0.1, 0.2])\nresult = values.dot(weights)  # ShapeError\n\n# after\nweights = pl.Series([0.1, 0.2, 0.3])\nassert len(weights) == len(values)\nresult = values.dot(weights)","handlingStrategy":"validation","validationCode":"if len(values) != len(weights):\n    raise ValueError(f\"length mismatch: {len(values)} vs {len(weights)}\")\nresult = values.dot(weights)","typeGuard":"def same_length(a, b) -> bool:\n    return len(a) == len(b)","tryCatchPattern":null,"preventionTips":["Assert equal lengths in helper functions that build DataFrames from dicts","Slice/filter all related series from the same source frame","Prefer Expr broadcasting (pl.lit) for scalar reuse instead of tiling lists"],"tags":["polars","series","shape-error","length-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}