Skip to content

Reshaping into invalid shapes has inconsistent error behavior #24807

@dsprenkels

Description

@dsprenkels

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

df = pl.DataFrame({"a": list(range(2 * 3 * 2))})

# This fails, because Polars cannot fill up to a window of 12
q = df.lazy().select(pl.col("a").reshape((12, 3)))
print(f"{q.collect(engine="in-memory") = }")
print(f"{q.collect(engine="streaming") = }")

# This does not fail, even though we have too many rows (4, not 2)
q = df.lazy().select(pl.col("a").reshape((2, 3)))
print(f"{q.collect(engine="in-memory") = }")
print(f"{q.collect(engine="streaming") = }")

Log output

Issue description

The error behavior of Expr.reshape() is inconsistent.

  • In case the resulting number of rows is too small to fill a single "reshape window", then Polars will raise an error.
  • In case the resulting number of rows is bigger than the "reshape window", Polars will ignore this and give back a dataframe with too many rows.

Expected behavior

Expr.reshape() should give an error if the number of rows does not exactly match the number of rows specified in the first reshape dimension.

Installed versions

--------Version info---------
Polars:              1.34.0
Index type:          UInt32
Platform:            Linux-6.14.0-33-generic-x86_64-with-glibc2.39
Python:              3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
Runtime:             unknown

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  1.8.0
altair               5.5.0
azure.identity       1.25.0
boto3                1.40.18
cloudpickle          3.1.1
connectorx           0.4.4
deltalake            1.1.4
fastexcel            0.15.1
fsspec               2025.9.0
gevent               25.8.2
google.auth          2.40.3
great_tables         0.18.0
matplotlib           3.10.6
numpy                2.2.6
openpyxl             3.1.5
pandas               2.3.2
polars_cloud         0.1.4
pyarrow              21.0.0
pydantic             2.11.9
pyiceberg            0.10.0
sqlalchemy           2.0.43
torch                <not installed>
xlsx2csv             0.8.4
xlsxwriter           3.2.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-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