{"record":{"id":"075482672127cbc3","repo":"pandas-dev/pandas","slug":"is-not-a-partition-because-intersection-is-not-nul","errorCode":null,"errorMessage":"Is not a partition because intersection is not null.","messagePattern":"Is not a partition because intersection is not null\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/scipy_sparse.py","lineNumber":35,"sourceCode":"from pandas.core.series import Series\n\nif TYPE_CHECKING:\n    from collections.abc import Iterable\n\n    import numpy as np\n    import scipy.sparse\n\n    from pandas._typing import (\n        IndexLabel,\n        npt,\n    )\n\n\ndef _check_is_partition(parts: Iterable, whole: Iterable) -> None:\n    whole = set(whole)\n    parts = [set(x) for x in parts]\n    if set.intersection(*parts) != set():\n        raise ValueError(\"Is not a partition because intersection is not null.\")\n    if set.union(*parts) != whole:\n        raise ValueError(\"Is not a partition because union is not the whole.\")\n\n\ndef _levels_to_axis(\n    ss,\n    levels: tuple[int] | list[int],\n    valid_ilocs: npt.NDArray[np.intp],\n    sort_labels: bool = False,\n) -> tuple[npt.NDArray[np.intp], list[IndexLabel]]:\n    \"\"\"\n    For a MultiIndexed sparse Series `ss`, return `ax_coords` and `ax_labels`,\n    where `ax_coords` are the coordinates along one of the two axes of the\n    destination sparse matrix, and `ax_labels` are the labels from `ss`' Index\n    which correspond to these coordinates.\n\n    Parameters\n    ----------","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/scipy_sparse.py#L17-L53","documentation":"Thrown by _check_is_partition in pandas/core/arrays/sparse/scipy_sparse.py:35 during sparse Series -> scipy COO matrix conversion. The row_levels and column_levels together must form a partition of the MultiIndex levels: every level appears in exactly one group. This error specifically fires when the two groups overlap (a level index is present in BOTH row_levels and column_levels), so their set intersection is non-empty.","triggerScenarios":"Calling ss.sparse.to_coo(row_levels=[0,1], column_levels=[1,2]) on a MultiIndexed sparse Series — level 1 is duplicated across groups. Any to_coo invocation where row_levels and column_levels share at least one level number.","commonSituations":"User wants a level to contribute to both axes (not supported by COO layout) and lists it in both. Miscounting level positions when the MultiIndex has 3+ levels. Copy-pasting a row_levels list into column_levels and forgetting to remove duplicates.","solutions":["Ensure row_levels and column_levels are disjoint: e.g. row_levels=[0], column_levels=[1,2].","If a level must inform both axes, precompute a derived level and reindex before to_coo rather than duplicating it.","Print the partition check before calling: assert set(row_levels).isdisjoint(column_levels)."],"exampleFix":"// before\nss.sparse.to_coo(row_levels=[0, 1], column_levels=[1, 2])  # overlap on level 1\n\n// after\nss.sparse.to_coo(row_levels=[0], column_levels=[1, 2])","handlingStrategy":"validation","validationCode":"def check_partition(row_levels, column_levels, nlevels):\n    row, col = set(row_levels), set(column_levels)\n    assert row.isdisjoint(col), f'overlap: {row & col}'\n    assert row | col == set(range(nlevels)), f'missing: {set(range(nlevels)) - (row|col)}'","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Treat row_levels and column_levels as a disjoint partition of range(ss.index.nlevels).","Compute one group from the other: column_levels = [l for l in range(nlevels) if l not in row_levels].","Never reuse a level number across both groups — COO layout forbids it."],"tags":["sparse","scipy","coo","multiindex","partition"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}