pandas-dev/pandas · error · ValueError

left and right must have the same length

Error message

left and right must have the same length

What it means

Raised in `_validate` when `len(left) != len(right)`. Interval endpoints are positionally paired; mismatched lengths cannot form intervals.

Solutions

  1. Align lengths before calling from_arrays: filter both sides with the same mask.
  2. Use `len(left) == len(right)` assertion upstream.
  3. If one side is shorter, pad or recompute the missing endpoints explicitly.

Example fix

# before
IntervalArray.from_arrays([0,1,2], [1,2])

# after
left = [0,1,2]; right = [1,2,3]
IntervalArray.from_arrays(left, right)
Defensive patterns

Strategy: validation

Validate before calling

def check_equal_length(left, right):
    if len(left) != len(right):
        raise ValueError(f'length mismatch: {len(left)} vs {len(right)}')
    return left, right

Type guard

def same_length(left, right) -> bool:
    return len(left) == len(right)

Try / catch

try:
    arr = IntervalArray.from_arrays(left, right)
except ValueError as e:
    if 'same length' in str(e):
        n = min(len(left), len(right))
        arr = IntervalArray.from_arrays(left[:n], right[:n])
    else:
        raise

Prevention

When it happens

Trigger: `IntervalArray.from_arrays([0,1,2], [1,2])`; left/right Series of different lengths after a filter applied to only one side.

Common situations: Filtering/grouping one bound but not the other; off-by-one in slicing; merging bounds from different-length sources.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/8c62c630fda41068. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/interval.py:612

    @classmethod
    def _validate(cls, left, right, dtype: IntervalDtype) -> None:
        """
        Verify that the IntervalArray is valid.

        Checks that

        * dtype is correct
        * left and right match lengths
        * left and right have the same missing values
        * left is always below right
        """
        if not isinstance(dtype, IntervalDtype):
            msg = f"invalid dtype: {dtype}"
            raise ValueError(msg)
        if len(left) != len(right):
            msg = "left and right must have the same length"
            raise ValueError(msg)
        left_mask = notna(left)
        right_mask = notna(right)
        if not (left_mask == right_mask).all():
            msg = (
                "missing values must be missing in the same "
                "location both left and right sides"
            )
            raise ValueError(msg)
        if not (left[left_mask] <= right[left_mask]).all():
            msg = "left side of interval must be <= right side"
            raise ValueError(msg)

    def _shallow_copy(self, left, right) -> Self:
        """
        Return a new IntervalArray with the replacement attributes

        Parameters
        ----------

View on GitHub (pinned to 3b7651241d)