{"record":{"id":"c710dff32d209e37","repo":"pola-rs/polars","slug":"height-of-data-self-height-does-not-match-spec","errorCode":null,"errorMessage":"height of data ({self.height}) does not match specified height ({height})","messagePattern":"height of data \\((.+?)\\) does not match specified height \\((.+?)\\)","errorType":"exception","errorClass":"ShapeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":496,"sourceCode":"\n        elif is_pycapsule(data):\n            self._df = pycapsule_to_frame(\n                data,\n                schema=schema,\n                schema_overrides=schema_overrides,\n            )._df\n        else:\n            msg = (\n                f\"DataFrame constructor called with unsupported type {type(data).__name__!r}\"\n                \" for the `data` parameter\"\n            )\n            raise TypeError(msg)\n\n        if height is not None and self.height != height:\n            from polars.exceptions import ShapeError\n\n            msg = f\"height of data ({self.height}) does not match specified height ({height})\"\n            raise ShapeError(msg)\n\n    @classmethod\n    def deserialize(\n        cls,\n        source: str | bytes | Path | IOBase,\n        *,\n        format: SerializationFormat = \"binary\",\n    ) -> DataFrame:\n        \"\"\"\n        Read a serialized DataFrame from a file.\n\n        Parameters\n        ----------\n        source\n            Path to a file or a file-like object (by file-like object, we refer to\n            objects that have a `read()` method, such as a file handler (e.g.\n            via builtin `open` function) or `BytesIO`).\n        format","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L478-L514","documentation":"After successful construction, the polars DataFrame constructor validates the optional `height` hint against the actual row count and raises polars.exceptions.ShapeError on mismatch. The parameter exists so internal fast paths (e.g. dict/records construction) can assert expected sizes cheaply; when supplied explicitly it must equal data's height.","triggerScenarios":"pl.DataFrame(data, height=5) where data has any other number of rows; explicitly passing height alongside from_records/from_dict style inputs; calling internal helpers that thread a height hint computed from a different (stale) dataset.","commonSituations":"Users copying constructor internals or using height as a 'truncate/pad to length' knob (it is not); stale height constants after the source data changed size; framework code that caches schema+height and replays them against refreshed data.","solutions":["Omit the height parameter entirely; polars derives it from the data","If you pass it, compute it from the same data: height=len(rows)","If you need fixed-size output, pad or slice the data explicitly before constructing"],"exampleFix":"# before\ndf = pl.DataFrame({'a': [1, 2, 3]}, height=5)\n\n# after\ndf = pl.DataFrame({'a': [1, 2, 3]})","handlingStrategy":"validation","validationCode":"if height is not None and hasattr(data, '__len__') and len(data) != height:\n    raise ValueError(f'expected {height} rows, data has {len(data)}; refusing to construct')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat DataFrame(height=...) as an internal assertion, not a user knob; simply omit it","When replaying cached schema metadata, recompute height from the current data","Use polars.testing.assert_frame_equal for size assertions in tests instead"],"tags":["dataframe-constructor","shape-mismatch","height"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}