Skip to content

Commit

Permalink
feat: improve error handling of column.stability when given a colum…
Browse files Browse the repository at this point in the history
…n that contains only None

Co-authored-by: patrikguempel <128832338+patrikguempel@users.noreply.github.com>
  • Loading branch information
alex-senger and patrikguempel committed Jun 23, 2023
1 parent 3520ce2 commit 35e9df8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ def stability(self) -> float:
\frac{\text{number of occurrences of most common non-null value}}{\text{number of non-null values}}
$$
The stability cannot be calculated for a column with only null values.
Returns
-------
stability : float
Expand All @@ -516,6 +518,10 @@ def stability(self) -> float:
"""
if self._data.size == 0:
raise ColumnSizeError("> 0", "0")

if self.all(lambda x: x is None):
raise ValueError("Stability cannot be calculated for a column with only null values.")

return self._data.value_counts()[self.mode()[0]] / self._data.count()

def standard_deviation(self) -> float:
Expand Down
10 changes: 8 additions & 2 deletions tests/safeds/data/tabular/containers/_column/test_stability.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ def test_should_return_stability_of_column(values: list[Any], expected: float) -
assert column.stability() == expected


def test_should_raise_if_column_is_empty() -> None:
def test_should_raise_column_size_error_if_column_is_empty() -> None:
column: Column[Any] = Column("A", [])
with pytest.raises(ColumnSizeError):
with pytest.raises(ColumnSizeError, match="Expected a column of size > 0 but got column of size 0."):
column.stability()


def test_should_raise_value_error_if_column_contains_only_none() -> None:
column: Column[Any] = Column("A", [None, None])
with pytest.raises(ValueError, match="Stability cannot be calculated for a column with only null values."):
column.stability()

0 comments on commit 35e9df8

Please sign in to comment.