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

fix(python): validate operator arithmetic with None, fix Series edge-case #13780

Merged
merged 3 commits into from
Jan 18, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 17, 2024

Adds test coverage for operator arithmetic against None (and tests the associated named methods). Discovered that Series was inconsistently raising an error where the same ops on Expr and DataFrame succeeded.

Note:
This may be an implicit regression of some kind (from the 0.19.x series) as it was found via unit testing while trying to upgrade our work codebase to 0.20.x 🤔

Example

All of the following formulations work consistently...

df = pl.DataFrame({"n": [2, 3]})

df.select( pl.col("n") * pl.lit(None) )
df.select( pl.col("n") * None )
df * pl.Series([None])
df * None

# shape: (2, 1)
# ┌──────┐
# │ n    │
# │ ---  │
# │ i64  │
# ╞══════╡
# │ null │
# │ null │
# └──────┘

df["n"] * pl.Series([None])

# shape: (2,)
# Series: 'n' [i64]
# [
#   null
#   null
# ]

... only this variant failed:

df["n"] * None
# `mul` operation not supported for dtype `null`

I've fixed this and added the missing test coverage.

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Jan 17, 2024
@alexander-beedie alexander-beedie changed the title fix(python): validate operator arithmetic with null, fix Series edge-case fix(python): validate operator arithmetic with None, fix Series edge-case Jan 17, 2024
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to support multiplying by None leading to a full None result, we should implement this on the Rust side and support multiplication and other operations for Null series.

I think this was 'broken' in the update where empty Series default to the Null type.

EDIT: I didn't read the description properly... maybe this is an OK fix.

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jan 18, 2024

I think this was 'broken' in the update where empty Series default to the Null type.

That worked before, and still works now ;)

EDIT: I didn't read the description properly... maybe this is an OK fix.

Indeed; operations against null already work in every single context (expr/frame/series), except for one very specific form; the fix simply makes this form equivalent to the null Series operation. The relevant Rust code already exists and works.

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, didn't give this the correct attention the first time around (it was late). This is a good fix!

I have one comment then this can be merged.

py-polars/polars/series/series.py Outdated Show resolved Hide resolved
Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge if CI is green 👍

@stinodego stinodego merged commit 2728e0b into pola-rs:main Jan 18, 2024
11 checks passed
@alexander-beedie alexander-beedie deleted the arithmetic-consistency-nulls branch January 18, 2024 09:00
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 22, 2024
…dge-case (pola-rs#13780)

Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
…dge-case (pola-rs#13780)

Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants