Skip to content

Commit

Permalink
feat: Added MissingDataError + Data processing tutorial (#406)
Browse files Browse the repository at this point in the history
Related to Issue #380 

### Summary of Changes

Added a new data processing tutorial

`from_rows` and `from_columns` now raise a `MissingDataError` if the
provided lists of rows/columns are empty
if none of the rows of a table in `filter_rows` match with the query the
function now returns an empty table with the same schema as the original
table

---------

Co-authored-by: Marsmaennchen221 <[email protected]>
Co-authored-by: WinPlay02 <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2023
1 parent 3162a66 commit 9a06181
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 56 deletions.
16 changes: 15 additions & 1 deletion Runtime/safe-ds/safeds/data/tabular/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ColumnSizeError,
DuplicateColumnNameError,
IndexOutOfBoundsError,
MissingDataError,
MissingSchemaError,
NonNumericColumnError,
SchemaMismatchError,
Expand Down Expand Up @@ -163,9 +164,14 @@ def from_rows(rows: list[Row]) -> Table:
Raises
------
MissingDataError
If an empty list is given.
SchemaMismatchError
If any of the row schemas does not match with the others.
"""
if len(rows) == 0:
raise MissingDataError("This function requires at least one row.")

schema_compare: TableSchema = rows[0].schema
row_array: list[Series] = []

Expand Down Expand Up @@ -195,9 +201,14 @@ def from_columns(columns: list[Column]) -> Table:
Raises
------
MissingDataError
If an empty list is given.
ColumnLengthMismatchError
If any of the column sizes does not match with the others.
"""
if len(columns) == 0:
raise MissingDataError("This function requires at least one column.")

dataframe: DataFrame = pd.DataFrame()

for column in columns:
Expand Down Expand Up @@ -405,7 +416,10 @@ def filter_rows(self, query: Callable[[Row], bool]) -> Table:
"""

rows: list[Row] = [row for row in self.to_rows() if query(row)]
result_table: Table = self.from_rows(rows)
if len(rows) == 0:
result_table = Table([], self.schema)
else:
result_table = self.from_rows(rows)
return result_table

def count_rows(self) -> int:
Expand Down
1 change: 1 addition & 0 deletions Runtime/safe-ds/safeds/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ColumnSizeError,
DuplicateColumnNameError,
IndexOutOfBoundsError,
MissingDataError,
MissingSchemaError,
NonNumericColumnError,
SchemaMismatchError,
Expand Down
9 changes: 9 additions & 0 deletions Runtime/safe-ds/safeds/exceptions/_data_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ class ColumnLengthMismatchError(Exception):

def __init__(self, column_info: str):
super().__init__(f"The length of at least one column differs: \n{column_info}")


class MissingDataError(Exception):
"""
Exception raised if a function is not given enough data to succeed.
"""

def __init__(self, missing_data_info: str):
super().__init__(f"The function is missing data: \n{missing_data_info}")
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd
import pytest
from safeds.data.tabular import Column, Table
from safeds.exceptions import MissingDataError


def test_from_columns() -> None:
Expand All @@ -11,3 +13,8 @@ def test_from_columns() -> None:
table_restored: Table = Table.from_columns(columns_table)

assert table_restored == table_expected


def test_from_columns_invalid() -> None:
with pytest.raises(MissingDataError):
Table.from_columns([])
7 changes: 7 additions & 0 deletions Runtime/safe-ds/tests/data/tabular/_row/test_from_rows.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from safeds.data.tabular import Row, Table
from safeds.exceptions import MissingDataError


def test_from_rows() -> None:
Expand All @@ -7,3 +9,8 @@ def test_from_rows() -> None:
table_is: Table = Table.from_rows(rows_is)

assert table_is == table_expected


def test_from_rows_invalid() -> None:
with pytest.raises(MissingDataError):
Table.from_rows([])
Loading

0 comments on commit 9a06181

Please sign in to comment.