Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in polars 1.14.0 results in corrupt dataframes when adding columns from literals #19957

Closed
2 tasks done
jakob-keller opened this issue Nov 24, 2024 · 1 comment · Fixed by #19964
Closed
2 tasks done
Assignees
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@jakob-keller
Copy link
Contributor

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

value = 1
values = [value] * 5
foo = pl.DataFrame({"foo": values})

# dataframe with column `bar` from Series
foo_with_bar_from_series = foo.with_columns(pl.Series("bar", values))
print(foo_with_bar_from_series)
"""
shape: (5, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
└─────┴─────┘
"""

# dataframe with column `bar` from literal
foo_with_bar_from_literal = foo.with_columns(pl.lit(value).alias("bar"))
print(foo_with_bar_from_literal)
"""
shape: (5, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ i64 ┆ i32 │
╞═════╪═════╡
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
└─────┴─────┘
"""

# both dataframes are equal
print(foo_with_bar_from_series.equals(foo_with_bar_from_literal))
"""
True
"""

# works as expected
print(foo_with_bar_from_series.gather_every(2))
"""
shape: (3, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
└─────┴─────┘
"""

# raises ShapeError since polars 1.14.0
print(foo_with_bar_from_literal.gather_every(2))
"""
polars.exceptions.ShapeError: Series length 3 doesn't match the DataFrame height of 5
"""

# weird work around exists
print(
    foo_with_bar_from_literal[:, : foo_with_bar_from_literal.width].gather_every(2),
)
"""
shape: (3, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ i64 ┆ i32 │
╞═════╪═════╡
│ 1   ┆ 1   │
│ 1   ┆ 1   │
│ 1   ┆ 1   │
└─────┴─────┘
"""

Log output

No response

Issue description

A regression introduced in polars 1.14.0 results in corrupt dataframes when adding columns from literals. Subsequent operations, such as .gather_every() result in ShapeError. See example for details, including a workaround that apparently fixes affected metadata.

Expected behavior

No ShapeError is thrown, i.e. behavior reverts to polars<1.14.0

Installed versions

--------Version info---------
Polars:              1.14.0
Index type:          UInt32
Platform:            macOS-15.1.1-arm64-arm-64bit
Python:              3.12.7 (main, Oct 16 2024, 07:12:08) [Clang 18.1.8 ]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
boto3                1.35.36
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
nest_asyncio         <not installed>
numpy                2.1.3
openpyxl             <not installed>
pandas               <not installed>
pyarrow              <not installed>
pydantic             2.10.1
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@jakob-keller jakob-keller added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Nov 24, 2024
@jakob-keller
Copy link
Contributor Author

I just noticed the different dtypes occuring in my example, but that is unrelated to the issue.

@ritchie46 ritchie46 self-assigned this Nov 25, 2024
@c-peters c-peters added the accepted Ready for implementation label Nov 25, 2024
@c-peters c-peters added this to Backlog Nov 25, 2024
@c-peters c-peters moved this to Done in Backlog Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants