-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get first cell with value other than
None
(#904)
Closes #799 ### Summary of Changes Added static method to Cell, which returns a Cell containing the polars expression of the first Cell in the list that is not None. Works on a list of Cells, regardless of Cell type (numeric, string, date, etc.). Returns None if given an empty list. --------- Co-authored-by: Saman Hushi <saman@hushalsadat.com> Co-authored-by: Lars Reimann <mail@larsreimann.com> Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
c485987
commit 5a0cdb3
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/safeds/data/tabular/containers/_cell/test_first_not_none.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from datetime import date, time | ||
|
||
import polars as pl | ||
import pytest | ||
from safeds.data.tabular.containers._cell import Cell | ||
from safeds.data.tabular.containers._lazy_cell import _LazyCell | ||
|
||
|
||
class TestFirstNotNone: | ||
def test_should_return_none(self) -> None: | ||
to_eval: list[Cell] = [_LazyCell(None) for i in range(5)] | ||
res = Cell.first_not_none(to_eval) | ||
assert res.eq(_LazyCell(None)) | ||
|
||
@pytest.mark.parametrize( | ||
("list_of_cells", "expected"), | ||
[ | ||
([_LazyCell(None), _LazyCell(1), _LazyCell(None), _LazyCell(4)], _LazyCell(1)), | ||
([_LazyCell(i) for i in range(5)], _LazyCell(1)), | ||
( | ||
[ | ||
_LazyCell(None), | ||
_LazyCell(None), | ||
_LazyCell(pl.lit("Hello, World!")), | ||
_LazyCell(pl.lit("Not returned")), | ||
], | ||
_LazyCell("Hello, World!"), | ||
), | ||
([_LazyCell(pl.lit(i)) for i in ["a", "b", "c", "d"]], _LazyCell(pl.lit("a"))), | ||
([_LazyCell(i) for i in [None, time(0, 0, 0, 0), None, time(1, 1, 1, 1)]], _LazyCell(time(0, 0, 0, 0))), | ||
( | ||
[_LazyCell(i) for i in [time(0, 0, 0, 0), time(1, 1, 1, 1), time(2, 2, 2, 2), time(3, 3, 3, 3)]], | ||
_LazyCell(time(0, 0, 0, 0)), | ||
), | ||
([_LazyCell(i) for i in [None, date(2000, 1, 1), date(1098, 3, 4), None]], _LazyCell(date(2000, 1, 1))), | ||
([_LazyCell(date(2000, 3, i)) for i in range(1, 5)], _LazyCell(date(2000, 3, 1))), | ||
([_LazyCell(i) for i in [None, pl.lit("a"), 1, time(0, 0, 0, 0)]], _LazyCell(pl.lit("a"))), | ||
([_LazyCell(i) for i in [time(1, 1, 1, 1), 0, pl.lit("c"), date(2020, 1, 7)]], _LazyCell(time(1, 1, 1, 1))), | ||
([], _LazyCell(None)), | ||
], | ||
ids=[ | ||
"numeric_with_null", | ||
"numeric_no_null", | ||
"strings_with_null", | ||
"strings_no_null", | ||
"times_with_null", | ||
"times_no_null", | ||
"dates_with_null", | ||
"dates_no_null", | ||
"mixed_with_null", | ||
"mixed_no_null", | ||
"empty_list", | ||
], | ||
) | ||
def test_should_return_first_non_none_value(self, list_of_cells: list[Cell], expected: Cell) -> None: | ||
res = Cell.first_not_none(list_of_cells) | ||
assert res.eq(expected) |