{"record":{"id":"162784ad0679f674","repo":"pandas-dev/pandas","slug":"length-of-values-len-data-does-not-match-leng","errorCode":null,"errorMessage":"Length of values ({len(data)}) does not match length of index ({len(index)})","messagePattern":"Length of values \\((.+?)\\) does not match length of index \\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/common.py","lineNumber":611,"sourceCode":"    ------\n    object : obj with modified attribute.\n    \"\"\"\n    if condition:\n        old_value = getattr(obj, attr)\n        setattr(obj, attr, value)\n    try:\n        yield obj\n    finally:\n        if condition:\n            setattr(obj, attr, old_value)\n\n\ndef require_length_match(data: Any, index: Index) -> None:\n    \"\"\"\n    Check the length of data matches the length of the index.\n    \"\"\"\n    if len(data) != len(index):\n        raise ValueError(\n            \"Length of values \"\n            f\"({len(data)}) \"\n            \"does not match length of index \"\n            f\"({len(index)})\"\n        )\n\n\n_cython_table = {\n    builtins.sum: \"sum\",\n    builtins.max: \"max\",\n    builtins.min: \"min\",\n    np.all: \"all\",\n    np.any: \"any\",\n    np.sum: \"sum\",\n    np.nansum: \"sum\",\n    np.mean: \"mean\",\n    np.nanmean: \"mean\",\n    np.prod: \"prod\",","sourceCodeStart":593,"sourceCodeEnd":629,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/common.py#L593-L629","documentation":"Raised by require_length_match when data passed to construct/assign into a Series/Index does not match the supplied Index length. pandas refuses to broadcast arbitrary-length data onto an index because silent truncation or padding would hide logic errors. The message reports both lengths so the mismatch is obvious.","triggerScenarios":"pd.Series([1,2,3], index=[10,20,30,40]); df['new'] = [1,2] on a 3-row frame; assign with a list whose len != len(df); construction with mismatched numpy arrays.","commonSituations":"Hardcoded lists that drift from data length; aggregations that change cardinality then get assigned back; off-by-one in slicing that produces N-1 values; groupby transforms that did not broadcast.","solutions":["Verify len(data) == len(index) before assignment.","Use an alignable Series instead of a list so pandas can reindex.","Broadcast a scalar explicitly: df['new'] = scalar.","Reset the index or reindex both sides to a common index before assigning."],"exampleFix":"// before\ndf['ratio'] = (df['a'] / df['a'].sum()).tolist()[1:]  # off by one\n// after\nratios = df['a'] / df['a'].sum()\nassert len(ratios) == len(df)\ndf['ratio'] = ratios","handlingStrategy":"validation","validationCode":"if len(data) != len(index):\n    raise ValueError(f'len(data)={len(data)} != len(index)={len(index)}')\ns = pd.Series(data, index=index)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always assert length match before assigning a list to a Series/DataFrame column.","Use Series (not list) inputs to leverage automatic alignment.","Reset/reindex both sides to a common index before assignment."],"tags":["value-error","length-mismatch","series","construction","assign"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}