Skip to content

Commit

Permalink
allow summing of duration in selection context (#4124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 22, 2022
1 parent bb45843 commit bd0d4a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions polars/polars-core/src/series/implementations/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,9 @@ impl SeriesTrait for SeriesWrap<DurationChunked> {
}

fn _sum_as_series(&self) -> Series {
Int32Chunked::full_null(self.name(), 1)
.cast(self.dtype())
.unwrap()
self.0.sum_as_series().into_duration(self.0.time_unit())
}

fn max_as_series(&self) -> Series {
self.0.max_as_series().into_duration(self.0.time_unit())
}
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,20 @@ def test_time_microseconds_3843() -> None:
def test_year_empty_df() -> None:
df = pl.DataFrame(pl.Series(name="date", dtype=pl.Date))
assert df.select(pl.col("date").dt.year()).dtypes == [pl.Int32]


def test_sum_duration() -> None:
assert pl.DataFrame(
[
{"name": "Jen", "duration": timedelta(seconds=60)},
{"name": "Mike", "duration": timedelta(seconds=30)},
{"name": "Jen", "duration": timedelta(seconds=60)},
]
).select(
[pl.col("duration").sum(), pl.col("duration").dt.seconds().alias("sec").sum()]
).to_dict(
False
) == {
"duration": [timedelta(seconds=150)],
"sec": [150],
}

0 comments on commit bd0d4a6

Please sign in to comment.