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) Summation on empty series evaluates to Some(0) #5773

Merged
merged 1 commit into from
Dec 12, 2022

Conversation

andrewpollack
Copy link
Contributor

@andrewpollack andrewpollack commented Dec 10, 2022

Closes: #5604

Changes:

  • Calling Series::sum() on an empty series now evaluates to Some(0).
  • Calling Series::sum_as_series() on an empty series now returns a Series with a single zero'd value. This is done to enable the above change in both Rust and Python libraries.
  • Adjusted documentation to reflect this change.
  • Added two unit tests to Python tests: test_arithmetic_empty , test_arithmetic_null .

I chose to add logic directly to sum_as_series() as to reduce the blast radius of this new logic. Please let me know if any changes are needed, including location of tests!

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Thanks @andrewpollack. I have left a few remarks.

polars/polars-core/src/series/mod.rs Outdated Show resolved Hide resolved
@@ -1038,6 +1047,23 @@ mod test {
let _ = series.slice(4, 2);
}

#[test]
fn empty_series_sum_evals_zero() {
Copy link
Member

Choose a reason for hiding this comment

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

Could these tests moved to the python tests? We want to reduce the testing ci time. Rust tests need a long time to compile/run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Looking to move now, and resolving why the Python test for an empty sum is still returning None

@andrewpollack andrewpollack force-pushed the empty-series-sum-zero branch 3 times, most recently from 31d701e to 1b1963c Compare December 11, 2022 18:20
@andrewpollack
Copy link
Contributor Author

andrewpollack commented Dec 11, 2022

From https://github.com/pola-rs/polars/blob/master/py-polars/src/series.rs#L482 , it looks like I'll need to adjust sum_as_series instead of base sum in order to have these changes reflect in the Python library. I've adjusted the PR to reflect this change

///
/// If the [`DataType`] is one of `{Int8, UInt8, Int16, UInt16}` the `Series` is
/// first cast to `Int64` to prevent overflow issues.
pub fn sum_as_series(&self) -> Series {
use DataType::*;
if self.is_empty() && self.dtype().is_numeric() {
return Series::new("", [0])
Copy link
Member

Choose a reason for hiding this comment

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

Almost there. Int8 | UInt8 | Int16 | UInt16 should be Int64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we recursively call into sum_as_series, won't they be converted to Int64 on the second call? Or should I implement this logic to save on an unnecessary call?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right. Missed that one.

Nope. This is good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome! Thank you for your feedbacks on this PR 😸

@andrewpollack andrewpollack changed the title Summation on empty series evaluates to Some(0) fix(rust,python) Summation on empty series evaluates to Some(0) Dec 11, 2022
@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Dec 11, 2022
@ritchie46
Copy link
Member

Thanks @andrewpollack 🙇

Merging in. 👍

@ritchie46 ritchie46 changed the title fix(rust,python) Summation on empty series evaluates to Some(0) fix(rust, python) Summation on empty series evaluates to Some(0) Dec 12, 2022
@ritchie46 ritchie46 merged commit 3de0592 into pola-rs:master Dec 12, 2022
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.

Empty Series sum to None
2 participants