From 35e9df8d1159314726e4a98081dd6fdf2efa823b Mon Sep 17 00:00:00 2001 From: alex-senger Date: Fri, 23 Jun 2023 15:24:00 +0200 Subject: [PATCH 1/2] feat: improve error handling of `column.stability` when given a column that contains only None Co-authored-by: patrikguempel <128832338+patrikguempel@users.noreply.github.com> --- src/safeds/data/tabular/containers/_column.py | 6 ++++++ .../data/tabular/containers/_column/test_stability.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/safeds/data/tabular/containers/_column.py b/src/safeds/data/tabular/containers/_column.py index ae42ace06..a4f198f06 100644 --- a/src/safeds/data/tabular/containers/_column.py +++ b/src/safeds/data/tabular/containers/_column.py @@ -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 @@ -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: diff --git a/tests/safeds/data/tabular/containers/_column/test_stability.py b/tests/safeds/data/tabular/containers/_column/test_stability.py index d1bb893d2..7de0050d9 100644 --- a/tests/safeds/data/tabular/containers/_column/test_stability.py +++ b/tests/safeds/data/tabular/containers/_column/test_stability.py @@ -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() From cf5d6f0a3e2a27b5f39ae02d7aea227b3074695c Mon Sep 17 00:00:00 2001 From: Alex Senger <91055000+alex-senger@users.noreply.github.com> Date: Fri, 23 Jun 2023 16:22:47 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Simon Breuer <86068340+sibre28@users.noreply.github.com> --- src/safeds/data/tabular/containers/_column.py | 4 ++-- .../safeds/data/tabular/containers/_column/test_stability.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/safeds/data/tabular/containers/_column.py b/src/safeds/data/tabular/containers/_column.py index a4f198f06..ec4392c6b 100644 --- a/src/safeds/data/tabular/containers/_column.py +++ b/src/safeds/data/tabular/containers/_column.py @@ -504,7 +504,7 @@ 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. + The stability is not definded for a column with only null values. Returns ------- @@ -520,7 +520,7 @@ def stability(self) -> float: 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.") + raise ValueError("Stability is not definded for a column with only null values.") return self._data.value_counts()[self.mode()[0]] / self._data.count() diff --git a/tests/safeds/data/tabular/containers/_column/test_stability.py b/tests/safeds/data/tabular/containers/_column/test_stability.py index 7de0050d9..abb9f346f 100644 --- a/tests/safeds/data/tabular/containers/_column/test_stability.py +++ b/tests/safeds/data/tabular/containers/_column/test_stability.py @@ -33,5 +33,5 @@ def test_should_raise_column_size_error_if_column_is_empty() -> None: 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."): + with pytest.raises(ValueError, match="Stability is not definded for a column with only null values."): column.stability()