{"record":{"id":"35685314e0d4073b","repo":"pola-rs/polars","slug":"cannot-create-dataframe-from-array-with-more-than","errorCode":null,"errorMessage":"cannot create DataFrame from array with more than two dimensions; shape = {shape}","messagePattern":"cannot create DataFrame from array with more than two dimensions; shape = (.+?)","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 more than two dimensions. A polars DataFrame is strictly 2-D (rows × columns); a (2, 3, 4) tensor has no unambiguous mapping, so the constructor raises this ValueError instead of guessing.","triggerScenarios":"pl.DataFrame(np.zeros((2, 3, 4))); passing model tensors shaped (batch, seq, features); arrays produced by np.stack of 2D matrices.","commonSituations":"ML preprocessing pipelines handing batched tensors to polars; image/video data with channel dimensions; forgetting to flatten features before logging experiment metrics into a DataFrame.","solutions":["Flatten to 2D: arr.reshape(-1, arr.shape[-1]) (stacking leading dims) or arr.reshape(arr.shape[0], -1).","Keep nesting as a List column: pl.DataFrame({\"tensor\": arr.tolist()}).","Create one DataFrame per 2D slice and pl.concat them with a batch-index column."],"exampleFix":"// before\ndf = pl.DataFrame(embeddings)  # shape (32, 10, 768)\n\n// after\ndf = pl.DataFrame(embeddings.reshape(-1, embeddings.shape[-1]))  # (320, 768)\n// or: df = pl.DataFrame({\"emb\": embeddings.tolist()})  # List(Float64) column","handlingStrategy":"validation","validationCode":"arr = np.asarray(value)\nif arr.ndim > 2:\n    arr = arr.reshape(-1, arr.shape[-1])  # or keep as list column: arr.tolist()\ndf = pl.DataFrame(arr)","typeGuard":"def is_dataframe_shaped(arr: np.ndarray) -> bool:\n    return arr.ndim <= 2","tryCatchPattern":null,"preventionTips":["Normalize tensor-shaped inputs with reshape at the boundary.","Use pl.DataFrame({\"col\": arr.tolist()}) when nesting must be preserved.","Document expected ndim on functions that accept raw arrays."],"tags":["numpy","ndim","reshape","dataframe"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}