Skip to content

Commit 7cd6cf1

Browse files
authored
fix: Allow structured conversion to/from numpy with Array types, preserving shape (#23438)
1 parent 5084fa1 commit 7cd6cf1

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

py-polars/polars/_utils/construction/dataframe.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,6 @@ def numpy_to_pydf(
12131213
n_columns = len(record_names)
12141214
for nm in record_names:
12151215
shape = data[nm].shape
1216-
if len(data[nm].shape) > 2:
1217-
msg = f"cannot create DataFrame from structured array with elements > 2D; shape[{nm!r}] = {shape}"
1218-
raise ValueError(msg)
12191216
if not schema:
12201217
schema = record_names
12211218
else:

py-polars/polars/dataframe/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ def to_numpy(
20102010
if s.dtype == String and not s.has_nulls():
20112011
arr = arr.astype(str, copy=False)
20122012
arrays.append(arr)
2013-
struct_dtype.append((s.name, arr.dtype))
2013+
struct_dtype.append((s.name, arr.dtype, arr.shape[1:]))
20142014

20152015
out = np.empty(self.height, dtype=struct_dtype)
20162016
for idx, c in enumerate(self.columns):

py-polars/tests/unit/interop/numpy/test_to_numpy_df.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,20 @@ def test_to_numpy_c_order_1700() -> None:
287287
df_chunked,
288288
pl.from_numpy(df_chunked.to_numpy(order="c"), schema=df_chunked.schema),
289289
)
290+
291+
292+
def test_to_numpy_array_shape_23426() -> None:
293+
df = pl.DataFrame(
294+
{
295+
"x": [1, 2],
296+
"y": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
297+
"z": [[[-1, -1, -2], [4, 5, 6]], [[-3, -5, -8], [10, 20, 30]]],
298+
},
299+
schema={
300+
"x": pl.UInt8,
301+
"y": pl.Array(pl.Float32, 3),
302+
"z": pl.Array(pl.Int16, (2, 3)),
303+
},
304+
)
305+
306+
assert_frame_equal(df, pl.from_numpy(df.to_numpy(structured=True)))

0 commit comments

Comments
 (0)