pandas-dev/pandas · error · ValueError

Is not a partition because intersection is not null.

Error message

Is not a partition because intersection is not null.

What it means

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.

Source

Thrown at pandas/core/arrays/sparse/scipy_sparse.py:35

from pandas.core.series import Series

if TYPE_CHECKING:
    from collections.abc import Iterable

    import numpy as np
    import scipy.sparse

    from pandas._typing import (
        IndexLabel,
        npt,
    )


def _check_is_partition(parts: Iterable, whole: Iterable) -> None:
    whole = set(whole)
    parts = [set(x) for x in parts]
    if set.intersection(*parts) != set():
        raise ValueError("Is not a partition because intersection is not null.")
    if set.union(*parts) != whole:
        raise ValueError("Is not a partition because union is not the whole.")


def _levels_to_axis(
    ss,
    levels: tuple[int] | list[int],
    valid_ilocs: npt.NDArray[np.intp],
    sort_labels: bool = False,
) -> tuple[npt.NDArray[np.intp], list[IndexLabel]]:
    """
    For a MultiIndexed sparse Series `ss`, return `ax_coords` and `ax_labels`,
    where `ax_coords` are the coordinates along one of the two axes of the
    destination sparse matrix, and `ax_labels` are the labels from `ss`' Index
    which correspond to these coordinates.

    Parameters
    ----------

View on GitHub (pinned to 3b7651241d)

Solutions

  1. Ensure row_levels and column_levels are disjoint: e.g. row_levels=[0], column_levels=[1,2].
  2. If a level must inform both axes, precompute a derived level and reindex before to_coo rather than duplicating it.
  3. Print the partition check before calling: assert set(row_levels).isdisjoint(column_levels).

Example fix

// before
ss.sparse.to_coo(row_levels=[0, 1], column_levels=[1, 2])  # overlap on level 1

// after
ss.sparse.to_coo(row_levels=[0], column_levels=[1, 2])
Defensive patterns

Strategy: validation

Validate before calling

def check_partition(row_levels, column_levels, nlevels):
    row, col = set(row_levels), set(column_levels)
    assert row.isdisjoint(col), f'overlap: {row & col}'
    assert row | col == set(range(nlevels)), f'missing: {set(range(nlevels)) - (row|col)}'

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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