Skip to content

DataFrame.to_numpy(structured=True) raises an exception for Array dtype #23426

@FBruzzesi

Description

@FBruzzesi

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl

(
    pl.DataFrame({"a": [[1], [2]]})
    .cast({"a": pl.Array(pl.Int32(), shape=(1, ))})
    .to_numpy(structured=True)
)

Log output

Issue description

If the datatype of a column is Array, then to_numpy with structured=True raises a ValueError.
This does not happen in:

  • Case of List type column
  • Case of DataFrame.to_numpy(structured=False)

Expected behavior

Same as List type.

From the example above, I would expect the output to be: array([(array([1]),), (array([2]),)], dtype=[('a', 'O')])

Installed versions

--------Version info---------
Polars:              1.31.0
Index type:          UInt32
Platform:            Linux-6.6.56+-x86_64-with-glibc2.35
Python:              3.11.11 (main, Dec  4 2024, 08:55:07) [GCC 11.4.0]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                1.38.11
cloudpickle          3.1.1
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2025.3.2
gevent               <not installed>
google.auth          2.40.1
great_tables         <not installed>
matplotlib           3.7.2
numpy                1.26.4
openpyxl             3.1.5
pandas               2.2.3
polars_cloud         <not installed>
pyarrow              19.0.1
pydantic             2.11.4
pyiceberg            <not installed>
sqlalchemy           2.0.40
torch                2.6.0+cu124
xlsx2csv             <not installed>
xlsxwriter           <not installed>```

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-interop-numpyArea: interoperability with NumPyA-panicArea: code that results in panic exceptionsP-mediumPriority: mediumacceptedReady for implementationbugSomething isn't workingpythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions