{"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":"validation","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/interval.py#L594-L630","documentation":"Raised in `_validate` when `len(left) != len(right)`. Interval endpoints are positionally paired; mismatched lengths cannot form intervals.","triggerScenarios":"`IntervalArray.from_arrays([0,1,2], [1,2])`; left/right Series of different lengths after a filter applied to only one side.","commonSituations":"Filtering/grouping one bound but not the other; off-by-one in slicing; merging bounds from different-length sources.","solutions":["Align lengths before calling from_arrays: filter both sides with the same mask.","Use `len(left) == len(right)` assertion upstream.","If one side is shorter, pad or recompute the missing endpoints explicitly."],"exampleFix":"# before\nIntervalArray.from_arrays([0,1,2], [1,2])\n\n# after\nleft = [0,1,2]; right = [1,2,3]\nIntervalArray.from_arrays(left, right)","handlingStrategy":"validation","validationCode":"def check_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 same_length(left, right) -> bool:\n    return len(left) == len(right)","tryCatchPattern":"try:\n    arr = IntervalArray.from_arrays(left, right)\nexcept ValueError as e:\n    if 'same length' in str(e):\n        n = min(len(left), len(right))\n        arr = IntervalArray.from_arrays(left[:n], right[:n])\n    else:\n        raise","preventionTips":["Apply the same filter/mask to both bound arrays.","Assert len(left) == len(right) before from_arrays.","Re-derive both bounds from a single aligned source."],"tags":["interval","length-mismatch","from-arrays","value-error"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}