Skip to content

Commit

Permalink
Merge branch 'main' into 266-user-facing-constructor-for-table
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann authored May 6, 2023
2 parents ad86a2f + dcf2e6c commit b3e0ff0
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

### Features

* `OneHotEncoder.inverse_transform` now maintains the column order from the original table ([#195](https://github.com/Safe-DS/Stdlib/issues/195)) ([3ec0041](https://github.com/Safe-DS/Stdlib/commit/3ec0041669ffe97640f96db345f3f43720d5c3f7)), closes [#109](https://github.com/Safe-DS/Stdlib/issues/109) [#109](https://github.com/Safe-DS/Stdlib/issues/109)
* `OneHotEncoder.inverse_transform` now maintains the column order from the original table ([#195](https://github.com/Safe-DS/Stdlib/issues/195)) ([3ec0041](https://github.com/Safe-DS/Stdlib/commit/3ec0041669ffe97640f96db345f3f43720d5c3f7)), closes [#109](https://github.com/Safe-DS/Stdlib/issues/109)
* add `plot_` prefix back to plotting methods ([#212](https://github.com/Safe-DS/Stdlib/issues/212)) ([e50c3b0](https://github.com/Safe-DS/Stdlib/commit/e50c3b0118825e33eef0e2a7073673603e316ee5)), closes [#211](https://github.com/Safe-DS/Stdlib/issues/211)
* adjust `Column`, `Schema` and `Table` to changes in `Row` ([#216](https://github.com/Safe-DS/Stdlib/issues/216)) ([ca3eebb](https://github.com/Safe-DS/Stdlib/commit/ca3eebbe2166f08d76cdcb89a012518ae8ff8e4e))
* back `Row` by a `polars.DataFrame` ([#214](https://github.com/Safe-DS/Stdlib/issues/214)) ([62ca34d](https://github.com/Safe-DS/Stdlib/commit/62ca34dd399da8b4e34b89bad408311707b53f41)), closes [#196](https://github.com/Safe-DS/Stdlib/issues/196) [#149](https://github.com/Safe-DS/Stdlib/issues/149)
Expand Down
27 changes: 27 additions & 0 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from collections.abc import Callable, Iterator

T = TypeVar("T")
R = TypeVar("R")


class Column(Sequence[T]):
Expand Down Expand Up @@ -300,6 +301,8 @@ def rename(self, new_name: str) -> Column:
"""
Return a new column with a new name.
This column is not modified.
Parameters
----------
new_name : str
Expand All @@ -312,6 +315,30 @@ def rename(self, new_name: str) -> Column:
"""
return Column._from_pandas_series(self._data.rename(new_name), self._type)

def transform(self, transformer: Callable[[T], R]) -> Column[R]:
"""
Apply a transform method to every data point.
This column is not modified.
Parameters
----------
transformer : Callable[[T], R]
Function that will be applied to all data points.
Returns
-------
transformed_column: Column
The transformed column.
Examples
--------
>>> from safeds.data.tabular.containers import Column
>>> price = Column("price", [4.99, 5.99, 2.49])
>>> sale = price.transform(lambda amount: amount * 0.8)
"""
return Column(self.name, self._data.apply(transformer, convert_dtype=True))

# ------------------------------------------------------------------------------------------------------------------
# Statistics
# ------------------------------------------------------------------------------------------------------------------
Expand Down
62 changes: 62 additions & 0 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def from_csv_file(path: str | Path) -> Table:
"""
Read data from a CSV file into a table.
This table is not modified.
Parameters
----------
path : str | Path
Expand Down Expand Up @@ -89,6 +91,8 @@ def from_excel_file(path: str | Path) -> Table:
"""
Read data from an Excel file into a table.
This table is not modified.
Parameters
----------
path : str | Path
Expand Down Expand Up @@ -117,6 +121,8 @@ def from_json_file(path: str | Path) -> Table:
"""
Read data from a JSON file into a table.
This table is not modified.
Parameters
----------
path : str | Path
Expand Down Expand Up @@ -144,6 +150,8 @@ def from_dict(data: dict[str, list[Any]]) -> Table:
"""
Create a table from a dictionary that maps column names to column values.
This table is not modified.
Parameters
----------
data : dict[str, list[Any]]
Expand All @@ -166,6 +174,8 @@ def from_columns(columns: list[Column]) -> Table:
"""
Return a table created from a list of columns.
This table is not modified.
Parameters
----------
columns : list[Column]
Expand Down Expand Up @@ -197,6 +207,8 @@ def from_rows(rows: list[Row]) -> Table:
"""
Return a table created from a list of rows.
This table is not modified.
Parameters
----------
rows : list[Row]
Expand Down Expand Up @@ -232,6 +244,8 @@ def _from_pandas_dataframe(data: pd.DataFrame, schema: Schema | None = None) ->
"""
Create a table from a `pandas.DataFrame`.
This table is not modified.
Parameters
----------
data : pd.DataFrame
Expand Down Expand Up @@ -483,6 +497,8 @@ def summary(self) -> Table:
"""
Return a table with a number of statistical key values.
This table is not modified.
Returns
-------
result : Table
Expand Down Expand Up @@ -528,6 +544,8 @@ def add_column(self, column: Column) -> Table:
"""
Return the original table with the provided column attached at the end.
This table is not modified.
Returns
-------
result : Table
Expand Down Expand Up @@ -557,6 +575,8 @@ def add_columns(self, columns: list[Column] | Table) -> Table:
"""
Add multiple columns to the table.
This table is not modified.
Parameters
----------
columns : list[Column] or Table
Expand Down Expand Up @@ -592,6 +612,8 @@ def add_row(self, row: Row) -> Table:
"""
Add a row to the table.
This table is not modified.
Parameters
----------
row : Row
Expand All @@ -614,6 +636,8 @@ def add_rows(self, rows: list[Row] | Table) -> Table:
"""
Add multiple rows to a table.
This table is not modified.
Parameters
----------
rows : list[Row] or Table
Expand Down Expand Up @@ -641,6 +665,8 @@ def filter_rows(self, query: Callable[[Row], bool]) -> Table:
"""
Return a table with rows filtered by Callable (e.g. lambda function).
This table is not modified.
Parameters
----------
query : lambda function
Expand All @@ -662,6 +688,8 @@ def keep_only_columns(self, column_names: list[str]) -> Table:
"""
Return a table with only the given column(s).
This table is not modified.
Parameters
----------
column_names : list[str]
Expand Down Expand Up @@ -692,6 +720,8 @@ def remove_columns(self, column_names: list[str]) -> Table:
"""
Return a table without the given column(s).
This table is not modified.
Parameters
----------
column_names : list[str]
Expand Down Expand Up @@ -722,6 +752,8 @@ def remove_columns_with_missing_values(self) -> Table:
"""
Return a table without the columns that contain missing values.
This table is not modified.
Returns
-------
table : Table
Expand All @@ -733,6 +765,8 @@ def remove_columns_with_non_numerical_values(self) -> Table:
"""
Return a table without the columns that contain non-numerical values.
This table is not modified.
Returns
-------
table : Table
Expand All @@ -745,6 +779,8 @@ def remove_duplicate_rows(self) -> Table:
"""
Return a copy of the table with every duplicate row removed.
This table is not modified.
Returns
-------
result : Table
Expand All @@ -758,6 +794,8 @@ def remove_rows_with_missing_values(self) -> Table:
"""
Return a table without the rows that contain missing values.
This table is not modified.
Returns
-------
table : Table
Expand All @@ -775,6 +813,8 @@ def remove_rows_with_outliers(self) -> Table:
Missing values are not considered outliers. They are also ignored during the calculation of the standard
deviation.
This table is not modified.
Returns
-------
new_table : Table
Expand All @@ -792,6 +832,8 @@ def rename_column(self, old_name: str, new_name: str) -> Table:
"""
Rename a single column.
This table is not modified.
Parameters
----------
old_name : str
Expand Down Expand Up @@ -826,6 +868,8 @@ def replace_column(self, old_column_name: str, new_column: Column) -> Table:
"""
Return a copy of the table with the specified old column replaced by a new column. Keeps the order of columns.
This table is not modified.
Parameters
----------
old_column_name : str
Expand Down Expand Up @@ -874,6 +918,8 @@ def shuffle_rows(self) -> Table:
"""
Shuffle the table randomly.
This table is not modified.
Returns
-------
result : Table
Expand All @@ -893,6 +939,8 @@ def slice_rows(
"""
Slice a part of the table into a new table.
This table is not modified.
Parameters
----------
start : int
Expand Down Expand Up @@ -942,6 +990,8 @@ def sort_columns(
If no comparator is given, the columns will be sorted alphabetically by their name.
This table is not modified.
Parameters
----------
comparator : Callable[[Column, Column], int]
Expand All @@ -967,6 +1017,8 @@ def sort_rows(self, comparator: Callable[[Row, Row], int]) -> Table:
* If `row1` should be ordered after `row2`, the function should return a positive number.
* If the original order of `row1` and `row2` should be kept, the function should return 0.
This table is not modified.
Parameters
----------
comparator : Callable[[Row, Row], int]
Expand All @@ -985,6 +1037,8 @@ def split(self, percentage_in_first: float) -> tuple[Table, Table]:
"""
Split the table into two new tables.
This table is not modified.
Parameters
----------
percentage_in_first : float
Expand All @@ -1009,6 +1063,8 @@ def tag_columns(self, target_name: str, feature_names: list[str] | None = None)
"""
Mark the columns of the table as target column or feature columns. The original table is not modified.
This table is not modified.
Parameters
----------
target_name : str
Expand All @@ -1029,6 +1085,8 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Tabl
"""
Transform provided column by calling provided transformer.
This table is not modified.
Returns
-------
result : Table
Expand All @@ -1050,6 +1108,8 @@ def transform_table(self, transformer: TableTransformer) -> Table:
"""
Apply a learned transformation onto this table.
This table is not modified.
Parameters
----------
transformer : TableTransformer
Expand Down Expand Up @@ -1084,6 +1144,8 @@ def inverse_transform_table(self, transformer: InvertibleTableTransformer) -> Ta
"""
Invert the transformation applied by the given transformer.
This table is not modified.
Parameters
----------
transformer : InvertibleTableTransformer
Expand Down
4 changes: 4 additions & 0 deletions src/safeds/data/tabular/transformation/_imputer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def fit(self, table: Table, column_names: list[str] | None) -> Imputer:
"""
Learn a transformation for a set of columns in a table.
This transformer is not modified.
Parameters
----------
table : Table
Expand Down Expand Up @@ -133,6 +135,8 @@ def transform(self, table: Table) -> Table:
"""
Apply the learned transformation to a table.
The table is not modified.
Parameters
----------
table : Table
Expand Down
6 changes: 6 additions & 0 deletions src/safeds/data/tabular/transformation/_label_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def fit(self, table: Table, column_names: list[str] | None) -> LabelEncoder:
"""
Learn a transformation for a set of columns in a table.
This transformer is not modified.
Parameters
----------
table : Table
Expand Down Expand Up @@ -53,6 +55,8 @@ def transform(self, table: Table) -> Table:
"""
Apply the learned transformation to a table.
The table is not modified.
Parameters
----------
table : Table
Expand Down Expand Up @@ -86,6 +90,8 @@ def inverse_transform(self, transformed_table: Table) -> Table:
"""
Undo the learned transformation.
The table is not modified.
Parameters
----------
transformed_table : Table
Expand Down
Loading

0 comments on commit b3e0ff0

Please sign in to comment.