Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added MissingDataError + Data processing tutorial #406

Merged
merged 7 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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