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
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ impl Series {
}

/// Compute the sum of all values in this Series.
/// Returns `None` if the array is empty or only contains null values.
/// Returns `Some(0)` if the array is empty, and `None` if the array only
/// contains null values.
///
/// If the [`DataType`] is one of `{Int8, UInt8, Int16, UInt16}` the `Series` is
/// first cast to `Int64` to prevent overflow issues.
Expand Down Expand Up @@ -511,11 +512,18 @@ impl Series {
}

/// Get the sum of the Series as a new Series of length 1.
/// Returns a Series with a single zeroed entry if self is an empty numeric series.
///
/// 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 😸

.cast(self.dtype())
.unwrap()
.sum_as_series();
}
match self.dtype() {
Int8 | UInt8 | Int16 | UInt16 => self.cast(&Int64).unwrap().sum_as_series(),
_ => self._sum_as_series(),
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ def test_arithmetic(s: pl.Series) -> None:
2**a


def test_arithmetic_empty() -> None:
series = pl.Series("a", [])
assert series.sum() == 0


def test_arithmetic_null() -> None:
series = pl.Series("a", [None])
assert series.sum() is None


def test_power() -> None:
a = pl.Series([1, 2], dtype=Int64)
b = pl.Series([None, 2.0], dtype=Float64)
Expand Down