Skip to content

Commit

Permalink
feat: Maintain tagging in methods inherited from Table class (#332)
Browse files Browse the repository at this point in the history
Closes #58.

### Summary of Changes

* Overrode methods in `TaggedTable` inherited from `Table` to maintain
tagging.
* Introduced new exceptions to be thrown when the overriden method
cannot be applied
* Added tests.

---------

Co-authored-by: megalinter-bot <[email protected]>
Co-authored-by: Alexander Gréus <[email protected]>
Co-authored-by: Alexander <[email protected]>
Co-authored-by: patrikguempel <[email protected]>
Co-authored-by: Simon Breuer <[email protected]>
  • Loading branch information
6 people authored Jul 9, 2023
1 parent 8e9065c commit bc73a6c
Show file tree
Hide file tree
Showing 40 changed files with 2,845 additions and 128 deletions.
47 changes: 41 additions & 6 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ def summary(self) -> Table:
# Transformations
# ------------------------------------------------------------------------------------------------------------------

# This method is meant as a way to "cast" instances of subclasses of `Table` to a proper `Table`, dropping any
# additional constraints that might have to hold in the subclass.
# Override accordingly in subclasses.
def _as_table(self: Table) -> Table:
"""
Transform the table to an instance of the Table class.
The original table is not modified.
Returns
-------
table: Table
The table, as an instance of the Table class.
"""
return self

def add_column(self, column: Column) -> Table:
"""
Return the original table with the provided column attached at the end.
Expand Down Expand Up @@ -888,8 +904,9 @@ def add_row(self, row: Row) -> Table:
"""
Add a row to the table.
If the table happens to be empty beforehand, respective columns will be added automatically.
This table is not modified.
If the table happens to be empty beforehand, respective features will be added automatically.
Parameters
----------
Expand Down Expand Up @@ -1077,6 +1094,8 @@ def keep_only_columns(self, column_names: list[str]) -> Table:
------
UnknownColumnNameError
If any of the given columns does not exist.
IllegalSchemaModificationError
If removing the columns would violate an invariant in the subclass.
Examples
--------
Expand Down Expand Up @@ -1120,6 +1139,8 @@ def remove_columns(self, column_names: list[str]) -> Table:
------
UnknownColumnNameError
If any of the given columns does not exist.
IllegalSchemaModificationError
If removing the columns would violate an invariant in the subclass.
Examples
--------
Expand Down Expand Up @@ -1158,6 +1179,11 @@ def remove_columns_with_missing_values(self) -> Table:
table : Table
A table without the columns that contain missing values.
Raises
------
IllegalSchemaModificationError
If removing the columns would violate an invariant in the subclass.
Examples
--------
>>> from safeds.data.tabular.containers import Table
Expand All @@ -1182,6 +1208,11 @@ def remove_columns_with_non_numerical_values(self) -> Table:
table : Table
A table without the columns that contain non-numerical values.
Raises
------
IllegalSchemaModificationError
If removing the columns would violate an invariant in the subclass.
Examples
--------
>>> from safeds.data.tabular.containers import Table
Expand Down Expand Up @@ -1331,7 +1362,9 @@ def rename_column(self, old_name: str, new_name: str) -> Table:

def replace_column(self, old_column_name: str, new_columns: list[Column]) -> Table:
"""
Return a copy of the table with the specified old column replaced by a list of new columns. Keeps the order of columns.
Return a copy of the table with the specified old column replaced by a list of new columns.
The order of columns is kept.
This table is not modified.
Expand All @@ -1352,12 +1385,12 @@ def replace_column(self, old_column_name: str, new_columns: list[Column]) -> Tab
------
UnknownColumnNameError
If the old column does not exist.
DuplicateColumnNameError
If at least one of the new columns already exists and the existing column is not affected by the replacement.
ColumnSizeError
If the size of at least one of the new columns does not match the amount of rows.
IllegalSchemaModificationError
If replacing the column would violate an invariant in the subclass.
Examples
--------
Expand Down Expand Up @@ -1475,7 +1508,7 @@ def sort_columns(
"""
Sort the columns of a `Table` with the given comparator and return a new `Table`.
The original table is not modified. The comparator is a function that takes two columns `col1` and `col2` and
The comparator is a function that takes two columns `col1` and `col2` and
returns an integer:
* If `col1` should be ordered before `col2`, the function should return a negative number.
Expand Down Expand Up @@ -1519,7 +1552,7 @@ def sort_rows(self, comparator: Callable[[Row, Row], int]) -> Table:
"""
Sort the rows of a `Table` with the given comparator and return a new `Table`.
The original table is not modified. The comparator is a function that takes two rows `row1` and `row2` and
The comparator is a function that takes two rows `row1` and `row2` and
returns an integer:
* If `row1` should be ordered before `row2`, the function should return a negative number.
Expand Down Expand Up @@ -1695,6 +1728,8 @@ def transform_table(self, transformer: TableTransformer) -> Table:
------
TransformerNotFittedError
If the transformer has not been fitted yet.
IllegalSchemaModificationError
If replacing the column would violate an invariant in the subclass.
Examples
--------
Expand Down
Loading

0 comments on commit bc73a6c

Please sign in to comment.