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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/polars-plan/src/logical_plan/aexpr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

field.coerce(DataType::Float64);
} else {
float_type(&mut field);
}
Ok(field)
},
Implode(expr) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_streaming_group_by_types() -> None:
"str_sum": pl.String,
"bool_first": pl.Boolean,
"bool_last": pl.Boolean,
"bool_mean": pl.Boolean,
"bool_mean": pl.Float64,
"bool_sum": pl.UInt32,
"date_sum": pl.Date,
"date_mean": pl.Date,
Expand Down