From 459ab7570c7c7b79304f78cab4f6bff82fc026a3 Mon Sep 17 00:00:00 2001 From: Alexander <47296670+Marsmaennchen221@users.noreply.github.com> Date: Sun, 16 Apr 2023 22:32:44 +0200 Subject: [PATCH] fix: `table.keep_only_columns` now maps column names to correct data (#194) Closes #115 . ### Summary of Changes Fixed `table.keep_only_columns` to rearrange the column names correctly Fixed grammar in two docs --------- Co-authored-by: Lars Reimann Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> --- src/safeds/data/tabular/containers/_table.py | 11 ++-- .../containers/_table/test_from_columns.py | 7 --- .../_table/test_keep_only_columns.py | 53 ++++++++++++++----- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 782ddb814..668325a63 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -129,14 +129,9 @@ 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: @@ -566,7 +561,7 @@ def keep_only_columns(self, column_names: list[str]) -> Table: Raises ------ ColumnNameError - If any of the given columns do not exist. + If any of the given columns does not exist. """ invalid_columns = [] column_indices = [] @@ -578,7 +573,7 @@ def keep_only_columns(self, column_names: list[str]) -> Table: if len(invalid_columns) != 0: raise UnknownColumnNameError(invalid_columns) transformed_data = self._data[column_indices] - transformed_data.columns = [name for name in self._schema.get_column_names() if name in column_names] + transformed_data.columns = column_names return Table(transformed_data) def remove_columns(self, column_names: list[str]) -> Table: @@ -598,7 +593,7 @@ def remove_columns(self, column_names: list[str]) -> Table: Raises ------ ColumnNameError - If any of the given columns do not exist. + If any of the given columns does not exist. """ invalid_columns = [] column_indices = [] diff --git a/tests/safeds/data/tabular/containers/_table/test_from_columns.py b/tests/safeds/data/tabular/containers/_table/test_from_columns.py index 7ce8add2f..34349a350 100644 --- a/tests/safeds/data/tabular/containers/_table/test_from_columns.py +++ b/tests/safeds/data/tabular/containers/_table/test_from_columns.py @@ -1,7 +1,5 @@ import pandas as pd -import pytest from safeds.data.tabular.containers import Column, Table -from safeds.data.tabular.exceptions import MissingDataError from tests.helpers import resolve_resource_path @@ -15,8 +13,3 @@ 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([]) diff --git a/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py b/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py index 35688d1b6..550e8ea7e 100644 --- a/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py +++ b/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py @@ -1,18 +1,45 @@ import pytest -from safeds.data.tabular.containers import Table +from safeds.data.tabular.containers import Column, Table from safeds.data.tabular.exceptions import UnknownColumnNameError -from tests.helpers import resolve_resource_path +class TestKeepOnlyColumns: + @pytest.mark.parametrize( + ("table", "column_names", "expected"), + [ + ( + Table.from_columns([Column("A", [1]), Column("B", [2])]), + [], + Table.from_columns([]), + ), + ( + Table.from_columns([Column("A", [1]), Column("B", [2])]), + ["A"], + Table.from_columns([Column("A", [1])]), + ), + ( + Table.from_columns([Column("A", [1]), Column("B", [2])]), + ["B"], + Table.from_columns([Column("B", [2])]), + ), + ( + Table.from_columns([Column("A", [1]), Column("B", [2])]), + ["A", "B"], + Table.from_columns([Column("A", [1]), Column("B", [2])]), + ), + # Related to https://github.com/Safe-DS/Stdlib/issues/115 + ( + Table.from_columns([Column("A", [1]), Column("B", [2]), Column("C", [3])]), + ["C", "A"], + Table.from_columns([Column("C", [3]), Column("A", [1])]), + ), + ], + ) + def test_should_keep_only_listed_columns(self, table: Table, column_names: list[str], expected: Table) -> None: + transformed_table = table.keep_only_columns(column_names) + assert transformed_table == expected -def test_keep_columns() -> None: - table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) - transformed_table = table.keep_only_columns(["A"]) - assert transformed_table.schema.has_column("A") - assert not transformed_table.schema.has_column("B") - - -def test_keep_columns_warning() -> None: - table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) - with pytest.raises(UnknownColumnNameError): - table.keep_only_columns(["C"]) + def test_should_raise_if_column_does_no_exist(self) -> None: + table = Table.from_columns([Column("A", [1]), Column("B", [2])]) + with pytest.raises(UnknownColumnNameError): + table.keep_only_columns(["C"])