{"record":{"id":"6d8c9b9388468f6a","repo":"pola-rs/polars","slug":"non-contiguous-buffer-must-be-made-contiguous","errorCode":null,"errorMessage":"non-contiguous buffer must be made contiguous","messagePattern":"non-contiguous buffer must be made contiguous","errorType":"exception","errorClass":"CopyNotAllowedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/interchange/buffer.py","lineNumber":36,"sourceCode":"\nclass PolarsBuffer(Buffer):\n    \"\"\"\n    A buffer object backed by a Polars Series consisting of a single chunk.\n\n    Parameters\n    ----------\n    data\n        The Polars Series backing the buffer object.\n    allow_copy\n        Allow data to be copied during operations on this column. If set to `False`,\n        a RuntimeError will be raised if data would be copied.\n    \"\"\"\n\n    def __init__(self, data: Series, *, allow_copy: bool = True) -> None:\n        if data.n_chunks() > 1:\n            if not allow_copy:\n                msg = \"non-contiguous buffer must be made contiguous\"\n                raise CopyNotAllowedError(msg)\n            data = data.rechunk()\n\n        self._data = data\n\n    @property\n    def bufsize(self) -> int:\n        \"\"\"Buffer size in bytes.\"\"\"\n        dtype = polars_dtype_to_dtype(self._data.dtype)\n\n        if dtype[0] == DtypeKind.BOOL:\n            _, offset, length = self._data._get_buffer_info()\n            n_bits = offset + length\n            n_bytes, rest = divmod(n_bits, 8)\n            # Round up to the nearest byte\n            if rest == 0:\n                return n_bytes\n            else:\n                return n_bytes + 1","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/py-polars/src/polars/interchange/buffer.py#L18-L54","documentation":"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.","triggerScenarios":"Thrown at py-polars/src/polars/interchange/buffer.py:36 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call .rechunk() on the Series or DataFrame before using the interchange buffer so the data is in a single contiguous chunk."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}