{"record":{"id":"6cd32eb7112fa195","repo":"pola-rs/polars","slug":"dataframe-dimensions-do-not-match","errorCode":null,"errorMessage":"DataFrame dimensions do not match","messagePattern":"DataFrame dimensions do not match","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":1110,"sourceCode":"    def _comp(self, other: Any, op: ComparisonOperator) -> DataFrame:\n        \"\"\"Compare a DataFrame with another object.\"\"\"\n        if isinstance(other, DataFrame):\n            return self._compare_to_other_df(other, op)\n        else:\n            return self._compare_to_non_df(other, op)\n\n    def _compare_to_other_df(\n        self,\n        other: DataFrame,\n        op: ComparisonOperator,\n    ) -> DataFrame:\n        \"\"\"Compare a DataFrame with another DataFrame.\"\"\"\n        if self.columns != other.columns:\n            msg = \"DataFrame columns do not match\"\n            raise ValueError(msg)\n        if self.shape != other.shape:\n            msg = \"DataFrame dimensions do not match\"\n            raise ValueError(msg)\n\n        suffix = \"__POLARS_CMP_OTHER\"\n        other_renamed = other.select(F.all().name.suffix(suffix))\n        combined = F.concat([self, other_renamed], how=\"horizontal\", strict=True)\n\n        if op == \"eq\":\n            expr = [F.col(n) == F.col(f\"{n}{suffix}\") for n in self.columns]\n        elif op == \"neq\":\n            expr = [F.col(n) != F.col(f\"{n}{suffix}\") for n in self.columns]\n        elif op == \"gt\":\n            expr = [F.col(n) > F.col(f\"{n}{suffix}\") for n in self.columns]\n        elif op == \"lt\":\n            expr = [F.col(n) < F.col(f\"{n}{suffix}\") for n in self.columns]\n        elif op == \"gt_eq\":\n            expr = [F.col(n) >= F.col(f\"{n}{suffix}\") for n in self.columns]\n        elif op == \"lt_eq\":\n            expr = [F.col(n) <= F.col(f\"{n}{suffix}\") for n in self.columns]\n        else:","sourceCodeStart":1092,"sourceCodeEnd":1128,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L1092-L1128","documentation":"After the column check passes, _compare_to_other_df requires equal shapes; a row-count difference makes element-wise comparison undefined and raises ValueError ('DataFrame dimensions do not match'). Polars will not broadcast row-wise between frames, so heights must be identical.","triggerScenarios":"df1 == df2 where one frame was filtered, deduplicated, sampled, or aggregated; comparing a full table to a group_by result; comparing against a head()/tail() slice; race where one side was appended to between construction and comparison.","commonSituations":"Assertion code comparing query output against a golden frame of different length; comparing pre/post-update snapshots; comparing a df to its distinct() version which shrank.","solutions":["Check heights first: df1.height == df2.height, and slice/align deliberately (e.g. both .head(n))","For row-order-insensitive comparison use df1.equals(df2.sort(df1.columns)) or join-based comparison","In tests use polars.testing.assert_frame_equal with check_row_order=False where appropriate","If lengths legitimately differ, compare on keys: df1.join(df2, on='id', how='inner') then compare joined columns"],"exampleFix":"# before\nresult = df1 == df2.head(5)  # df1 has 10 rows -> ValueError\n\n# after\nresult = df1.head(5) == df2.head(5)","handlingStrategy":"validation","validationCode":"if df.height != other.height:\n    raise ValueError(f'row counts differ: {df.height} vs {other.height}; align before comparing')\nresult = df == other","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compare heights before element-wise frame comparison","Slice both frames to a common n (head) when intent is prefix comparison","Use join-on-key comparison when row sets legitimately differ"],"tags":["comparison","shape-mismatch","dataframe-equality"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}