{"record":{"id":"aabf73be77449e68","repo":"pola-rs/polars","slug":"dataframe-columns-do-not-match","errorCode":null,"errorMessage":"DataFrame columns do not match","messagePattern":"DataFrame columns do not match","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":1107,"sourceCode":"\n        return PolarsDataFrame(self, allow_copy=allow_copy)\n\n    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]","sourceCodeStart":1089,"sourceCodeEnd":1125,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L1089-L1125","documentation":"Element-wise comparison of two DataFrames (df1 == df2, !=, <, >, ...) goes through _compare_to_other_df, which first requires identical columns: same names, same order. Since column order changes the positional pairing of the comparison, a mismatch (including mere reordering) raises ValueError instead of comparing wrong pairs.","triggerScenarios":"df1 == df2 after df2 was built with a different column order or renamed columns; comparing a df to a filtered/selected version (df2 = df[['b', 'a']]); comparing against a join result whose column order differs.","commonSituations":"Test assertions comparing expected vs actual frames where construction order drifted; comparing a df to its sorted-by-column version; pipelines where one side passed through a select/rename.","solutions":["Align first: df2 = df2.select(df1.columns) then compare","Rename mismatched columns before comparing: df2 = df2.rename({'old': 'new'})","For order-insensitive equality testing use df1.equals(df2) or assert_frame_equal(left, right, check_column_order=False)","For exact equality semantics use df1.equals(df2) which also checks dtypes"],"exampleFix":"# before\nresult = df1 == df2.select(['b', 'a'])  # ValueError\n\n# after\ndf2 = df2.select(df1.columns)\nresult = df1 == df2","handlingStrategy":"validation","validationCode":"if list(df.columns) != list(other.columns):\n    other = other.select(df.columns)  # reorder/project to match\nassert list(df.columns) == list(other.columns)\nresult = df == other","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize column order with other.select(df.columns) before element-wise comparison","Use polars.testing.assert_frame_equal(check_column_order=False) in tests instead of == checks","Watch for column reordering after select/join operations in pipelines"],"tags":["comparison","columns","dataframe-equality","pandas-migration"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}