{"record":{"id":"79fc562e9bdb9e6d","repo":"pola-rs/polars","slug":"cannot-create-dataframe-from-zero-dimensional-arra","errorCode":null,"errorMessage":"cannot create DataFrame from zero-dimensional array","messagePattern":"cannot create DataFrame from zero-dimensional array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/dataframe.py","lineNumber":1278,"sourceCode":"                    orient = \"col\"\n                    n_columns = n_schema_cols\n                else:\n                    orient = \"row\"\n                    n_columns = shape[1]\n\n            elif orient == \"row\":\n                n_columns = shape[1]\n            elif orient == \"col\":\n                n_columns = shape[0]\n            else:\n                msg = f\"`orient` must be one of {{'col', 'row', None}}, got {orient!r}\"\n                raise ValueError(msg)\n        else:\n            if shape == ():\n                msg = \"cannot create DataFrame from zero-dimensional array\"\n            else:\n                msg = f\"cannot create DataFrame from array with more than two dimensions; shape = {shape}\"\n            raise ValueError(msg)\n\n    if schema is not None and len(schema) != n_columns:\n        if (n_schema_cols := len(schema)) != 1:\n            msg = f\"dimensions of `schema` ({n_schema_cols}) must match data dimensions ({n_columns})\"\n            raise ValueError(msg)\n        n_columns = n_schema_cols\n\n    column_names, schema_overrides = _unpack_schema(\n        schema, schema_overrides=schema_overrides, n_expected=n_columns\n    )\n\n    # Convert data to series\n    if structured_array:\n        data_series = [\n            pl.Series(\n                name=series_name,\n                values=data[record_name],\n                dtype=schema_overrides.get(record_name),","sourceCodeStart":1260,"sourceCodeEnd":1296,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/construction/dataframe.py#L1260-L1296","documentation":"numpy_to_pydf rejects NumPy arrays with ndim == 0 (shape ()). A zero-dimensional array is a bare scalar (e.g. np.float64(3.0)) and has neither rows nor columns, so it cannot back a DataFrame. Only 1D and 2D arrays are constructible.","triggerScenarios":"pl.DataFrame(np.float64(5)); pl.DataFrame(np.array(1.0)); passing the result of aggregations like np.asarray(df[\"a\"].sum()) or a single cell extracted with .item() wrapped back into np.array.","commonSituations":"Reductions (arr.sum(), np.mean(...)) returning scalars that flow into generic conversion code; iterating over data of unknown shape where a scalar slips through instead of a length-1 sequence.","solutions":["Wrap the scalar in a sequence: pl.DataFrame([value]) or pl.DataFrame({\"col\": [value]}).","Normalize the input: np.atleast_1d(arr) before passing.","If the value came from .item(), keep the original array or use [arr.item()] instead."],"exampleFix":"// before\nval = arr.sum()  # np.float64, shape ()\ndf = pl.DataFrame(val)\n\n// after\ndf = pl.DataFrame({\"total\": [float(arr.sum())]})\n// or: df = pl.DataFrame(np.atleast_1d(arr.sum()))","handlingStrategy":"validation","validationCode":"arr = np.asarray(value)\nif arr.ndim == 0:\n    arr = np.atleast_1d(arr)  # or: value = [value]\ndf = pl.DataFrame(arr)","typeGuard":"def is_constructible_ndarray(arr: np.ndarray) -> bool:\n    return 1 <= arr.ndim <= 2","tryCatchPattern":null,"preventionTips":["Run np.atleast_1d on values from reductions before construction.","Never pass .item() results back into the constructor without a list wrapper.","Assert arr.ndim in (1, 2) in data-loading helpers."],"tags":["numpy","zero-dim","scalar","dataframe"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}