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, python): include null count in rolling window validity with min_periods #13863

Merged
merged 3 commits into from
Jan 22, 2024

Conversation

mcrumiller
Copy link
Contributor

Resolves #13856.

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jan 19, 2024
@@ -86,7 +93,7 @@ def test_rolling_map_sum_int_cast_to_float() -> None:
function=lambda s: s.sum(), window_size=3, weights=[1.0, 2.0, 3.0]
)

expected = pl.Series("A", [None, None, 32.0, 20.0, 48.0], dtype=pl.Float64)
expected = pl.Series("A", [None, None, 32.0, None, None], dtype=pl.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.

This test was previously incorrect, as it did not take into account the null values in the window.

Some(5.0),
Some(11.0)
]
&[None, None, Some(3.0), None, None, None, None,]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was also previously incorrect.

@@ -111,6 +111,12 @@ mod inner_mod {
// we are in bounds
let arr_window = unsafe { arr.slice_typed_unchecked(start, size) };

// ensure we still meet window size criteria after removing null values
if size - arr_window.null_count() < options.min_periods {
Copy link
Contributor Author

@mcrumiller mcrumiller Jan 20, 2024

Choose a reason for hiding this comment

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

I realize that the null count is more efficiently calculated by only tracking the head and tail of each new window (i.e. decrementing if a null drops off and incrementing if a new null pops up), but because this is the slow rolling_map this is almost guaranteed not to be a bottleneck.

Copy link
Member

Choose a reason for hiding this comment

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

Nah.. Indeed. This might be the worst code in the repo. :(

@ritchie46 ritchie46 merged commit 6b1fcaa into pola-rs:main Jan 22, 2024
22 checks passed
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
@mcrumiller mcrumiller deleted the rolling-map-nulls branch January 26, 2024 16:16
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 rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rolling_map() not using nulls to determine window size with min_periods
2 participants