{"record":{"id":"8c62c630fda41068","repo":"pandas-dev/pandas","slug":"left-and-right-must-have-the-same-length","errorCode":null,"errorMessage":"left and right must have the same length","messagePattern":"left and right must have the same length","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":612,"sourceCode":"\n    @classmethod\n    def _validate(cls, left, right, dtype: IntervalDtype) -> None:\n        \"\"\"\n        Verify that the IntervalArray is valid.\n\n        Checks that\n\n        * dtype is correct\n        * left and right match lengths\n        * left and right have the same missing values\n        * left is always below right\n        \"\"\"\n        if not isinstance(dtype, IntervalDtype):\n            msg = f\"invalid dtype: {dtype}\"\n            raise ValueError(msg)\n        if len(left) != len(right):\n            msg = \"left and right must have the same length\"\n            raise ValueError(msg)\n        left_mask = notna(left)\n        right_mask = notna(right)\n        if not (left_mask == right_mask).all():\n            msg = (\n                \"missing values must be missing in the same \"\n                \"location both left and right sides\"\n            )\n            raise ValueError(msg)\n        if not (left[left_mask] <= right[left_mask]).all():\n            msg = \"left side of interval must be <= right side\"\n            raise ValueError(msg)\n\n    def _shallow_copy(self, left, right) -> Self:\n        \"\"\"\n        Return a new IntervalArray with the replacement attributes\n\n        Parameters\n        ----------","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L594-L630","documentation":"Raised by `_validate` when `len(left) != len(right)`. Intervals are pairwise, so the two bound arrays must align element-by-element. Fires at pandas/core/arrays/interval.py:612.","triggerScenarios":"`pd.IntervalIndex.from_arrays([0,1,2], [1,2])`, or after filtering/sorting one column independently of the other.","commonSituations":"Dropping NA from one side but not the other; groupby transforms that change length on only one column; off-by-one in break computation via `from_breaks`.","solutions":["Align both arrays to a common index before constructing: `left, right = left.align(right)`.","Recompute breaks so `len(breaks) == n+1` if using `from_breaks`.","Trim or pad explicitly after asserting the intended length matches."],"exampleFix":"// before\npd.IntervalIndex.from_arrays(df['lo'].dropna(), df['hi'])\n// after\nlo, hi = df['lo'].align(df['hi'])\npd.IntervalIndex.from_arrays(lo, hi)","handlingStrategy":"validation","validationCode":"def equal_length(left, right):\n    if len(left) != len(right):\n        raise ValueError(f\"length mismatch: {len(left)} vs {len(right)}\")\n    return left, right","typeGuard":"def lengths_match(left, right) -> bool:\n    return len(left) == len(right)","tryCatchPattern":"try:\n    ia = pd.IntervalArray(left, right)\nexcept ValueError as e:\n    if \"same length\" in str(e):\n        n = min(len(left), len(right))\n        ia = pd.IntervalArray(left[:n], right[:n])\n    else:\n        raise","preventionTips":["Use df[['lo','hi']].dropna() together so both sides stay aligned.","Run len(left) == len(right) assertions in test fixtures.","Prefer from_arrays over from_breaks unless breaks are deliberate."],"tags":["interval","length-mismatch","validation","alignment"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}