Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions py-polars/polars/_utils/construction/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,6 @@ def numpy_to_pydf(
n_columns = len(record_names)
for nm in record_names:
shape = data[nm].shape
if len(data[nm].shape) > 2:
msg = f"cannot create DataFrame from structured array with elements > 2D; shape[{nm!r}] = {shape}"
raise ValueError(msg)
if not schema:
schema = record_names
else:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ def to_numpy(
if s.dtype == String and not s.has_nulls():
arr = arr.astype(str, copy=False)
arrays.append(arr)
struct_dtype.append((s.name, arr.dtype))
struct_dtype.append((s.name, arr.dtype, arr.shape[1:]))

out = np.empty(self.height, dtype=struct_dtype)
for idx, c in enumerate(self.columns):
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/interop/numpy/test_to_numpy_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,20 @@ def test_to_numpy_c_order_1700() -> None:
df_chunked,
pl.from_numpy(df_chunked.to_numpy(order="c"), schema=df_chunked.schema),
)


def test_to_numpy_array_shape_23426() -> None:
df = pl.DataFrame(
{
"x": [1, 2],
"y": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
"z": [[[-1, -1, -2], [4, 5, 6]], [[-3, -5, -8], [10, 20, 30]]],
},
schema={
"x": pl.UInt8,
"y": pl.Array(pl.Float32, 3),
"z": pl.Array(pl.Int16, (2, 3)),
},
)

assert_frame_equal(df, pl.from_numpy(df.to_numpy(structured=True)))
Loading