{"record":{"id":"ed443e76c869fa56","repo":"pandas-dev/pandas","slug":"name-from-tuples-requires-tuples-of-length-2-go","errorCode":null,"errorMessage":"{name}.from_tuples requires tuples of length 2, got {d}","messagePattern":"(.+?)\\.from_tuples requires tuples of length 2, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":586,"sourceCode":"        Length: 2, dtype: interval[int64, right]\n        \"\"\"\n        if len(data):\n            left, right = [], []\n        else:\n            # ensure that empty data keeps input dtype\n            left = right = data\n\n        for d in data:\n            if not isinstance(d, tuple) and isna(d):\n                lhs = rhs = np.nan\n            else:\n                name = cls.__name__\n                try:\n                    # need list of length 2 tuples, e.g. [(0, 1), (1, 2), ...]\n                    lhs, rhs = d\n                except ValueError as err:\n                    msg = f\"{name}.from_tuples requires tuples of length 2, got {d}\"\n                    raise ValueError(msg) from err\n                except TypeError as err:\n                    msg = f\"{name}.from_tuples received an invalid item, {d}\"\n                    raise TypeError(msg) from err\n            left.append(lhs)\n            right.append(rhs)\n\n        return cls.from_arrays(left, right, closed, copy=False, dtype=dtype)\n\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","sourceCodeStart":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L568-L604","documentation":"Raised by `IntervalArray.from_tuples` / `IntervalIndex.from_tuples` when unpacking an entry as `lhs, rhs = d` fails with a ValueError because the tuple has a length other than 2 (e.g., a 3-tuple or 1-tuple). Each input must be exactly a pair. Fires at pandas/core/arrays/interval.py:586.","triggerScenarios":"`pd.IntervalIndex.from_tuples([(0,1,2), (3,4)])`, or rows from a DataFrame with 3+ columns: `from_tuples(df[['a','b','c']].itertuples(...))`.","commonSituations":"Passing `zip(a, b, c)` by accident, or itertuples including the index as the first element.","solutions":["Project to exactly two columns: `df[['low','high']].itertuples(index=False, name=None)`.","Filter/repair malformed tuples before calling: `[t for t in data if isinstance(t, tuple) and len(t) == 2]`.","Use `from_arrays(left, right)` instead when bounds already live in two sequences."],"exampleFix":"// before\npd.IntervalIndex.from_tuples(df[['lo','mid','hi']].itertuples(index=False, name=None))\n// after\npd.IntervalIndex.from_tuples(df[['lo','hi']].itertuples(index=False, name=None))","handlingStrategy":"validation","validationCode":"def clean_tuples(data):\n    out = []\n    for d in data:\n        if isinstance(d, tuple) and len(d) == 2:\n            out.append(d)\n        elif isinstance(d, tuple):\n            raise ValueError(f\"tuple of length {len(d)} is not 2: {d!r}\")\n    return out","typeGuard":"def all_pairs(data) -> bool:\n    return all(isinstance(d, tuple) and len(d) == 2 for d in data)","tryCatchPattern":"try:\n    ii = pd.IntervalIndex.from_tuples(data)\nexcept ValueError as e:\n    if \"requires tuples of length 2\" in str(e):\n        ii = pd.IntervalIndex.from_tuples([t for t in data if isinstance(t, tuple) and len(t) == 2])\n    else:\n        raise","preventionTips":["Use from_arrays when bounds already live in two sequences.","Project exactly two columns: df[['lo','hi']].itertuples(index=False, name=None).","Validate tuple shapes before calling from_tuples."],"tags":["interval","from-tuples","input-validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}