{"record":{"id":"cfd027343b65ede8","repo":"pandas-dev/pandas","slug":"lengths-must-match-to-compare","errorCode":null,"errorMessage":"Lengths must match to compare","messagePattern":"Lengths must match to compare","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":719,"sourceCode":"        self._left[key] = value_left\n        self._right[key] = value_right\n\n    def _cmp_method(self, other, op):\n        # ensure pandas array for list-like and eliminate non-interval scalars\n        if is_list_like(other):\n            if not isinstance(\n                other, (list, np.ndarray, ExtensionArray)\n            ) and not ops.has_castable_attr(other):\n                warnings.warn(\n                    f\"Operation with {type(other).__name__} is deprecated. \"\n                    \"In a future version these will be treated as scalar-like. \"\n                    \"To retain the old behavior, explicitly wrap in a Series \"\n                    \"instead.\",\n                    Pandas4Warning,\n                    stacklevel=find_stack_level(),\n                )\n            if len(self) != len(other):\n                raise ValueError(\"Lengths must match to compare\")\n            other = pd_array(other)\n        elif not isinstance(other, Interval):\n            # non-interval scalar -> no matches\n            if other is NA:\n                # GH#31882\n                from pandas.core.arrays import BooleanArray\n\n                arr = np.empty(self.shape, dtype=bool)\n                mask = np.ones(self.shape, dtype=bool)\n                return BooleanArray(arr, mask)\n            return invalid_comparison(self, other, op)\n\n        # determine the dtype of the elements we want to compare\n        if isinstance(other, Interval):\n            other_dtype = pandas_dtype(\"interval\")\n        elif not isinstance(other.dtype, CategoricalDtype):\n            other_dtype = other.dtype\n        else:","sourceCodeStart":701,"sourceCodeEnd":737,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L701-L737","documentation":"Raised by `_cmp_method` (the engine behind `==`, `!=`, `<`, etc.) when comparing against a list-like whose length differs from the IntervalArray. Broadcasting-style comparisons are not allowed for unequal-length array operands. Fires at pandas/core/arrays/interval.py:719.","triggerScenarios":"`ia == [pd.Interval(0,1)]` where `len(ia) != 1`, or comparing two Series of intervals of different lengths.","commonSituations":"Comparing an interval column against a single-element list (intended as scalar), or after filtering one side.","solutions":["Wrap scalar comparisons as a scalar: `ia == pd.Interval(0,1)` (no list).","Align both sides: `ia.align(other)` or reindex to a common index.","Broadcast explicitly: `ia == np.repeat(other, len(ia))` when you really mean elementwise-repeat."],"exampleFix":"// before\nia == [pd.Interval(0, 1)]\n// after\nia == pd.Interval(0, 1)","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef compare_interval(ia, other):\n    if pd.api.types.is_list_like(other) and not isinstance(other, pd.Interval):\n        if len(other) != len(ia):\n            raise ValueError(f\"length mismatch: {len(ia)} vs {len(other)}\")\n    return ia == other","typeGuard":"import pandas as pd\nfrom pandas.api.types import is_list_like\n\ndef lengths_match_or_scalar(ia, other) -> bool:\n    return not is_list_like(other) or len(other) == len(ia)","tryCatchPattern":"try:\n    result = ia == other\nexcept ValueError as e:\n    if \"Lengths must match\" in str(e):\n        result = ia == pd.Interval(other[0].left, other[0].right)  # if intent was scalar\n    else:\n        raise","preventionTips":["Compare against a scalar Interval when checking membership of a single value.","Align both Series to a common index before comparing arrays.","Avoid wrapping single intervals in lists."],"tags":["interval","comparison","length-mismatch","broadcasting"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}