diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 9747d5813..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: 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 0d4b7f5ac..6b376d742 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,29 +1,46 @@ import pytest + from safeds.data.tabular.containers import Table, Column from safeds.data.tabular.exceptions import UnknownColumnNameError -from tests.helpers import resolve_resource_path - - -def test_keep_only_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_only_columns_order() -> None: - table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) - transformed_table = table.keep_only_columns(["B", "A"]) - assert transformed_table == Table.from_columns( +class TestKeepOnlyColumns: + @pytest.mark.parametrize( + ("table", "column_names", "expected"), [ - Column("B", [2]), - Column("A", [1]) + ( + 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): + transformed_table = table.keep_only_columns(column_names) + assert transformed_table == expected - -def test_keep_only_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_raise_if_column_does_no_exist(self): + table = Table.from_columns([Column("A", [1]), Column("B", [2])]) + with pytest.raises(UnknownColumnNameError): + table.keep_only_columns(["C"])