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(rust): make 100 * pl.col(pl.Boolean).mean() work #13725

Merged
merged 3 commits into from
Jan 23, 2024

Conversation

Wainberg
Copy link
Contributor

Closes #7651 based on the implementation suggested by @advoet here.

Before:

pl.DataFrame({"a": [True, True, True, False, False]}).select(100 * pl.col("a").mean())
shape: (1, 1)
┌─────────┐
│ literal │
│ ---     │
│ i32     │
╞═════════╡
│ 0       │
└─────────┘

After:

>>> pl.DataFrame({"a": [True, True, True, False, False]}).select(100 * pl.col("a").mean())
shape: (1, 1)
┌─────────┐
│ literal │
│ ---     │
│ f64     │
╞═════════╡
│ 60.0    │
└─────────┘

@@ -127,7 +127,11 @@ impl AExpr {
Mean(expr) => {
let mut field =
arena.get(*expr).to_field(schema, Context::Default, arena)?;
float_type(&mut field);
if matches!(&field.dtype, DataType::Boolean) {
Copy link
Member

@ritchie46 ritchie46 Jan 15, 2024

Choose a reason for hiding this comment

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

This branch already happens in the float_type call?

Edit. I see that there are two float_type functions that are inconsistent. Boolean should be added to the float_type function, not as an exclusion here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll be honest, I just blindly copied @advoet's code from the linked issue. But it does fix the bug.

Copy link
Member

Choose a reason for hiding this comment

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

It does fix it, but it is a bandaid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to implement a more robust fix, just let me know where the "right" place to change would be. It's an important fix because people like to use 100 * boolean_column.mean() to calculate percentages.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a lot of cases where boolean is treated as a u8 in these types of operations. It might be worth adding is_numeric_or_bool() for those many cases scattered throughout the codebase (perhaps different PR).

Copy link
Contributor

Choose a reason for hiding this comment

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

Eh, I just perused through and it feels like it's not worth it. Go ahead with this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just not familiar enough with the Rust codebase to know what a non-bandaid fix would look like.

Copy link
Contributor

@mcrumiller mcrumiller Jan 19, 2024

Choose a reason for hiding this comment

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

He just means at the very top of the file do this instead of your else clause below:

fn float_type(field: &mut Field) {
    if (field.dtype.is_numeric() || field.dtype == DataType::Boolean)
        && field.dtype != DataType::Float32
    {
        field.coerce(DataType::Float64)
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ohhh I didn't see Ritchie's edit to his original post! thanks :)

Copy link
Contributor

@mcrumiller mcrumiller Jan 19, 2024

Choose a reason for hiding this comment

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

He may have been noting the other float_type function in expr_dyn_fn.rs but this is actually never used.

@Wainberg Wainberg changed the title fix(rust): make 100 * pl.col(pl.Boolean).mean() work' fix(rust): make 100 * pl.col(pl.Boolean).mean() work Jan 15, 2024
@Wainberg
Copy link
Contributor Author

@ritchie46 updated this PR based on your suggestion:

Edit. I see that there are two float_type functions that are inconsistent. Boolean should be added to the float_type function, not as an exclusion here.

@ritchie46 ritchie46 merged commit 88475d2 into pola-rs:main Jan 23, 2024
22 checks passed
@Wainberg Wainberg deleted the boolean-mean branch January 23, 2024 07:52
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
Co-authored-by: Wainberg <m.wainberg@utoronto.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

100 * pl.col("a").mean() returns 0 when a is a boolean column
3 participants