pola-rs/polars · error · CopyNotAllowedError

non-contiguous buffer must be made contiguous

Error message

non-contiguous buffer must be made contiguous

What it means

Raised during PolarsBuffer construction (the dataframe interchange protocol) when the backing Series spans multiple chunks and allow_copy=False. Multi-chunk data is not contiguous in memory, so exposing it as a single buffer would require rechunking (a copy), which the caller explicitly forbade. Fix by passing allow_copy=True or rechunking the Series first.

Solutions

  1. Call .rechunk() on the Series or DataFrame before using the interchange buffer so the data is in a single contiguous chunk.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at py-polars/src/polars/interchange/buffer.py:36 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/6d8c9b9388468f6a. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/interchange/buffer.py:36

class PolarsBuffer(Buffer):
    """
    A buffer object backed by a Polars Series consisting of a single chunk.

    Parameters
    ----------
    data
        The Polars Series backing the buffer object.
    allow_copy
        Allow data to be copied during operations on this column. If set to `False`,
        a RuntimeError will be raised if data would be copied.
    """

    def __init__(self, data: Series, *, allow_copy: bool = True) -> None:
        if data.n_chunks() > 1:
            if not allow_copy:
                msg = "non-contiguous buffer must be made contiguous"
                raise CopyNotAllowedError(msg)
            data = data.rechunk()

        self._data = data

    @property
    def bufsize(self) -> int:
        """Buffer size in bytes."""
        dtype = polars_dtype_to_dtype(self._data.dtype)

        if dtype[0] == DtypeKind.BOOL:
            _, offset, length = self._data._get_buffer_info()
            n_bits = offset + length
            n_bytes, rest = divmod(n_bits, 8)
            # Round up to the nearest byte
            if rest == 0:
                return n_bytes
            else:
                return n_bytes + 1

View on GitHub (pinned to 9b5d73fd00)