diff --git a/src/safeds/data/tabular/containers/_row.py b/src/safeds/data/tabular/containers/_row.py index 85fd3482f..4dac07763 100644 --- a/src/safeds/data/tabular/containers/_row.py +++ b/src/safeds/data/tabular/containers/_row.py @@ -3,7 +3,7 @@ import pandas as pd from IPython.core.display_functions import DisplayHandle, display -from safeds.data.tabular.typing import ColumnType, TableSchema +from safeds.data.tabular.typing import ColumnType, Schema from safeds.exceptions import UnknownColumnNameError @@ -15,13 +15,13 @@ class Row: ---------- data : typing.Iterable The data. - schema : TableSchema + schema : Schema The schema of the row. """ - def __init__(self, data: typing.Iterable, schema: TableSchema): + def __init__(self, data: typing.Iterable, schema: Schema): self._data: pd.Series = data if isinstance(data, pd.Series) else pd.Series(data) - self.schema: TableSchema = schema + self.schema: Schema = schema self._data = self._data.reset_index(drop=True) def __getitem__(self, column_name: str) -> Any: @@ -145,7 +145,5 @@ def _ipython_display_(self) -> DisplayHandle: tmp = self._data.to_frame().T tmp.columns = self.get_column_names() - with pd.option_context( - "display.max_rows", tmp.shape[0], "display.max_columns", tmp.shape[1] - ): + with pd.option_context("display.max_rows", tmp.shape[0], "display.max_columns", tmp.shape[1]): return display(tmp) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index a3fed3a4f..d8d80ee5a 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -11,7 +11,7 @@ import seaborn as sns from IPython.core.display_functions import DisplayHandle, display from pandas import DataFrame, Series -from safeds.data.tabular.typing import ColumnType, TableSchema +from safeds.data.tabular.typing import ColumnType, Schema from safeds.exceptions import ( ColumnLengthMismatchError, ColumnSizeError, @@ -40,7 +40,7 @@ class Table: ---------- data : typing.Iterable The data. - schema : Optional[TableSchema] + schema : Optional[Schema] The schema of the table. If not specified, the schema will be inferred from the data. Raises @@ -174,7 +174,7 @@ def from_rows(rows: list[Row]) -> Table: if len(rows) == 0: raise MissingDataError("This function requires at least one row.") - schema_compare: TableSchema = rows[0].schema + schema_compare: Schema = rows[0].schema row_array: list[Series] = [] for row in rows: @@ -190,9 +190,9 @@ def from_rows(rows: list[Row]) -> Table: # Dunder methods # ------------------------------------------------------------------------------------------------------------------ - def __init__(self, data: Iterable, schema: Optional[TableSchema] = None): + def __init__(self, data: Iterable, schema: Optional[Schema] = None): self._data: pd.Dataframe = data if isinstance(data, pd.DataFrame) else pd.DataFrame(data) - self._schema: TableSchema = TableSchema._from_dataframe(self._data) if schema is None else schema + self._schema: Schema = Schema._from_dataframe(self._data) if schema is None else schema if self._data.empty: self._data = pd.DataFrame(columns=self._schema.get_column_names()) @@ -227,13 +227,13 @@ def __str__(self) -> str: # ------------------------------------------------------------------------------------------------------------------ @property - def schema(self) -> TableSchema: + def schema(self) -> Schema: """ Return the schema of the table. Returns ------- - schema : TableSchema + schema : Schema The schema. """ return self._schema diff --git a/src/safeds/data/tabular/containers/_tagged_table.py b/src/safeds/data/tabular/containers/_tagged_table.py index 065b8c9be..6113d55f3 100644 --- a/src/safeds/data/tabular/containers/_tagged_table.py +++ b/src/safeds/data/tabular/containers/_tagged_table.py @@ -2,7 +2,7 @@ from IPython.core.display_functions import DisplayHandle from safeds.data.tabular.containers import Column, Table -from safeds.data.tabular.typing import TableSchema +from safeds.data.tabular.typing import Schema class TaggedTable(Table): @@ -17,7 +17,7 @@ class TaggedTable(Table): Name of the target column. feature_names : Optional[list[str]] Names of the feature columns. If None, all columns except the target column are used. - schema : Optional[TableSchema] + schema : Optional[Schema] The schema of the table. If not specified, the schema will be inferred from the data. """ @@ -26,7 +26,7 @@ def __init__( data: Iterable, target_name: str, feature_names: Optional[list[str]] = None, - schema: Optional[TableSchema] = None, + schema: Optional[Schema] = None, ): super().__init__(data, schema) diff --git a/src/safeds/data/tabular/typing/__init__.py b/src/safeds/data/tabular/typing/__init__.py index 81e890666..b77250d6a 100644 --- a/src/safeds/data/tabular/typing/__init__.py +++ b/src/safeds/data/tabular/typing/__init__.py @@ -1,2 +1,2 @@ from ._column_type import Anything, Boolean, ColumnType, Integer, RealNumber, String -from ._table_schema import TableSchema +from ._schema import Schema diff --git a/src/safeds/data/tabular/typing/_table_schema.py b/src/safeds/data/tabular/typing/_schema.py similarity index 92% rename from src/safeds/data/tabular/typing/_table_schema.py rename to src/safeds/data/tabular/typing/_schema.py index cee295b77..26a5cb7b0 100644 --- a/src/safeds/data/tabular/typing/_table_schema.py +++ b/src/safeds/data/tabular/typing/_schema.py @@ -8,9 +8,9 @@ @dataclass -class TableSchema: +class Schema: """ - Store column names and corresponding data types for a table. + Store column names and corresponding data types for a `Table` or `Row`. Parameters ---------- @@ -80,7 +80,7 @@ def _get_column_index_by_name(self, column_name: str) -> int: return list(self._schema.keys()).index(column_name) @staticmethod - def _from_dataframe(dataframe: pd.DataFrame) -> TableSchema: + def _from_dataframe(dataframe: pd.DataFrame) -> Schema: """ Construct a TableSchema from a Dataframe. This function is not supposed to be exposed to the user. @@ -91,7 +91,7 @@ def _from_dataframe(dataframe: pd.DataFrame) -> TableSchema: Returns ------- - _from_dataframe: TableSchema + _from_dataframe: Schema The constructed TableSchema. """ @@ -100,7 +100,7 @@ def _from_dataframe(dataframe: pd.DataFrame) -> TableSchema: # noinspection PyProtectedMember types = (ColumnType._from_numpy_dtype(dtype) for dtype in dataframe.dtypes) - return TableSchema(dict(zip(names, types))) + return Schema(dict(zip(names, types))) def get_column_names(self) -> list[str]: """ @@ -132,7 +132,7 @@ def __repr__(self) -> str: return self.__str__() def __eq__(self, o: object) -> bool: - if not isinstance(o, TableSchema): + if not isinstance(o, Schema): return NotImplemented if self is o: return True diff --git a/tests/safeds/data/tabular/containers/_row/test_count.py b/tests/safeds/data/tabular/containers/_row/test_count.py index c5051dd5f..e00f787d0 100644 --- a/tests/safeds/data/tabular/containers/_row/test_count.py +++ b/tests/safeds/data/tabular/containers/_row/test_count.py @@ -1,10 +1,10 @@ from safeds.data.tabular.containers import Row -from safeds.data.tabular.typing import Integer, String, TableSchema +from safeds.data.tabular.typing import Integer, Schema, String def test_count() -> None: row = Row( [0, "1"], - TableSchema({"testColumn1": Integer(), "testColumn2": String()}), + Schema({"testColumn1": Integer(), "testColumn2": String()}), ) assert row.count() == 2 diff --git a/tests/safeds/data/tabular/containers/_row/test_get_column_names.py b/tests/safeds/data/tabular/containers/_row/test_get_column_names.py index ebe52fbbc..c65d37922 100644 --- a/tests/safeds/data/tabular/containers/_row/test_get_column_names.py +++ b/tests/safeds/data/tabular/containers/_row/test_get_column_names.py @@ -1,12 +1,12 @@ import pandas as pd from safeds.data.tabular.containers import Row -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_get_column_names() -> None: row = Row( pd.Series(data=[1, 2]), - TableSchema( + Schema( { "col1": RealNumber(), "col2": RealNumber(), @@ -17,5 +17,5 @@ def test_get_column_names() -> None: def test_get_column_names_empty() -> None: - row = Row(pd.Series(data=[]), TableSchema({})) + row = Row(pd.Series(data=[]), Schema({})) assert not row.get_column_names() diff --git a/tests/safeds/data/tabular/containers/_row/test_iter.py b/tests/safeds/data/tabular/containers/_row/test_iter.py index f9180bd34..e75f441ef 100644 --- a/tests/safeds/data/tabular/containers/_row/test_iter.py +++ b/tests/safeds/data/tabular/containers/_row/test_iter.py @@ -1,10 +1,10 @@ from safeds.data.tabular.containers import Row -from safeds.data.tabular.typing import Integer, String, TableSchema +from safeds.data.tabular.typing import Integer, Schema, String def test_iter() -> None: row = Row( [0, "1"], - TableSchema({"testColumn1": Integer(), "testColumn2": String()}), + Schema({"testColumn1": Integer(), "testColumn2": String()}), ) assert list(row) == ["testColumn1", "testColumn2"] diff --git a/tests/safeds/data/tabular/containers/_row/test_len.py b/tests/safeds/data/tabular/containers/_row/test_len.py index eec9468f3..0fe97359b 100644 --- a/tests/safeds/data/tabular/containers/_row/test_len.py +++ b/tests/safeds/data/tabular/containers/_row/test_len.py @@ -1,10 +1,10 @@ from safeds.data.tabular.containers import Row -from safeds.data.tabular.typing import Integer, String, TableSchema +from safeds.data.tabular.typing import Integer, Schema, String def test_count() -> None: row = Row( [0, "1"], - TableSchema({"testColumn1": Integer(), "testColumn2": String()}), + Schema({"testColumn1": Integer(), "testColumn2": String()}), ) assert len(row) == 2 diff --git a/tests/safeds/data/tabular/containers/_table/test_add_row.py b/tests/safeds/data/tabular/containers/_table/test_add_row.py index 37a35af5e..b31310794 100644 --- a/tests/safeds/data/tabular/containers/_table/test_add_row.py +++ b/tests/safeds/data/tabular/containers/_table/test_add_row.py @@ -1,7 +1,7 @@ import pandas as pd from _pytest.python_api import raises from safeds.data.tabular.containers import Row, Table -from safeds.data.tabular.typing import Integer, String, TableSchema +from safeds.data.tabular.typing import Integer, Schema, String from safeds.exceptions import SchemaMismatchError @@ -19,6 +19,6 @@ def test_add_row_invalid() -> None: table1 = Table(pd.DataFrame(data={"col1": [1, 2, 1], "col2": [1, 2, 4]})) row = Row( pd.Series(data=[5, "Hallo"]), - TableSchema({"col1": Integer(), "col2": String()}), + Schema({"col1": Integer(), "col2": String()}), ) table1 = table1.add_row(row) diff --git a/tests/safeds/data/tabular/containers/_table/test_get_column_names.py b/tests/safeds/data/tabular/containers/_table/test_get_column_names.py index 9750dd620..e1e64a2ce 100644 --- a/tests/safeds/data/tabular/containers/_table/test_get_column_names.py +++ b/tests/safeds/data/tabular/containers/_table/test_get_column_names.py @@ -1,6 +1,6 @@ import pandas as pd from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import TableSchema +from safeds.data.tabular.typing import Schema def test_get_column_names() -> None: @@ -9,5 +9,5 @@ def test_get_column_names() -> None: def test_get_column_names_empty() -> None: - table = Table(pd.DataFrame(), TableSchema({})) + table = Table(pd.DataFrame(), Schema({})) assert not table.get_column_names() diff --git a/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_missing_values.py b/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_missing_values.py index f67a2bac7..6f70b9c41 100644 --- a/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_missing_values.py +++ b/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_missing_values.py @@ -1,6 +1,6 @@ import pandas as pd from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_remove_columns_with_missing_values_valid() -> None: @@ -19,6 +19,6 @@ def test_remove_columns_with_missing_values_valid() -> None: def test_remove_columns_with_missing_values_empty() -> None: - table = Table([], TableSchema({"col1": RealNumber()})) + table = Table([], Schema({"col1": RealNumber()})) updated_table = table.remove_columns_with_missing_values() assert updated_table.get_column_names() == ["col1"] diff --git a/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_non_numerical_values.py b/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_non_numerical_values.py index 63686d087..648be68e8 100644 --- a/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_non_numerical_values.py +++ b/tests/safeds/data/tabular/containers/_table/test_remove_columns_with_non_numerical_values.py @@ -1,6 +1,6 @@ import pandas as pd from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_remove_columns_with_non_numerical_values_valid() -> None: @@ -19,6 +19,6 @@ def test_remove_columns_with_non_numerical_values_valid() -> None: def test_remove_columns_with_non_numerical_values_empty() -> None: - table = Table([], TableSchema({"col1": RealNumber()})) + table = Table([], Schema({"col1": RealNumber()})) updated_table = table.remove_columns_with_non_numerical_values() assert updated_table.get_column_names() == ["col1"] diff --git a/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_missing_values.py b/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_missing_values.py index 4c1f7f68c..43815e21e 100644 --- a/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_missing_values.py +++ b/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_missing_values.py @@ -1,6 +1,6 @@ import pandas as pd from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_remove_rows_with_missing_values_valid() -> None: @@ -19,6 +19,6 @@ def test_remove_rows_with_missing_values_valid() -> None: def test_remove_rows_with_missing_values_empty() -> None: - table = Table([], TableSchema({"col1": RealNumber()})) + table = Table([], Schema({"col1": RealNumber()})) updated_table = table.remove_rows_with_missing_values() assert updated_table.get_column_names() == ["col1"] diff --git a/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_outliers.py b/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_outliers.py index 906085a84..eb4174b41 100644 --- a/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_outliers.py +++ b/tests/safeds/data/tabular/containers/_table/test_remove_rows_with_outliers.py @@ -1,6 +1,6 @@ import pandas as pd from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_remove_rows_with_outliers_no_outliers() -> None: @@ -59,7 +59,7 @@ def test_remove_rows_with_outliers_with_outliers() -> None: def test_remove_rows_with_outliers_no_rows() -> None: - table = Table([], TableSchema({"col1": RealNumber()})) + table = Table([], Schema({"col1": RealNumber()})) result = table.remove_rows_with_outliers() assert result.count_rows() == 0 assert result.count_columns() == 1 diff --git a/tests/safeds/data/tabular/containers/_table/test_table.py b/tests/safeds/data/tabular/containers/_table/test_table.py index 778acd5f1..5ab205e4c 100644 --- a/tests/safeds/data/tabular/containers/_table/test_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_table.py @@ -1,9 +1,9 @@ from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import RealNumber, TableSchema +from safeds.data.tabular.typing import RealNumber, Schema def test_create_empty_table() -> None: - table = Table([], TableSchema({"col1": RealNumber()})) + table = Table([], Schema({"col1": RealNumber()})) col = table.get_column("col1") assert col.count() == 0 assert isinstance(col.type, RealNumber) @@ -12,4 +12,4 @@ def test_create_empty_table() -> None: def test_create_empty_table_without_schema() -> None: table = Table([]) - assert table.schema == TableSchema({}) + assert table.schema == Schema({}) diff --git a/tests/safeds/data/tabular/containers/_table/test_to_rows.py b/tests/safeds/data/tabular/containers/_table/test_to_rows.py index 864fff425..27f5e4a12 100644 --- a/tests/safeds/data/tabular/containers/_table/test_to_rows.py +++ b/tests/safeds/data/tabular/containers/_table/test_to_rows.py @@ -1,12 +1,12 @@ import pandas as pd from safeds.data.tabular.containers import Row, Table -from safeds.data.tabular.typing import Integer, String, TableSchema +from safeds.data.tabular.typing import Integer, Schema, String from tests.helpers import resolve_resource_path def test_to_rows() -> None: table = Table.from_csv_file(resolve_resource_path("test_row_table.csv")) - expected_schema: TableSchema = TableSchema( + expected_schema: Schema = Schema( { "A": Integer(), "B": Integer(), diff --git a/tests/safeds/data/tabular/typing/_table_schema/__init__.py b/tests/safeds/data/tabular/typing/_schema/__init__.py similarity index 100% rename from tests/safeds/data/tabular/typing/_table_schema/__init__.py rename to tests/safeds/data/tabular/typing/_schema/__init__.py diff --git a/tests/safeds/data/tabular/typing/_table_schema/test__str__.py b/tests/safeds/data/tabular/typing/_schema/test__str__.py similarity index 100% rename from tests/safeds/data/tabular/typing/_table_schema/test__str__.py rename to tests/safeds/data/tabular/typing/_schema/test__str__.py diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_get_column_index_by_name.py b/tests/safeds/data/tabular/typing/_schema/test_get_column_index_by_name.py similarity index 55% rename from tests/safeds/data/tabular/typing/_table_schema/test_get_column_index_by_name.py rename to tests/safeds/data/tabular/typing/_schema/test_get_column_index_by_name.py index 4ac1f291a..9671b326d 100644 --- a/tests/safeds/data/tabular/typing/_table_schema/test_get_column_index_by_name.py +++ b/tests/safeds/data/tabular/typing/_schema/test_get_column_index_by_name.py @@ -4,7 +4,4 @@ def test_get_column_index_by_name() -> None: table = Table(pd.DataFrame(data={"col1": [1], "col2": [2]})) - assert ( - table.schema._get_column_index_by_name("col1") == 0 - and table.schema._get_column_index_by_name("col2") == 1 - ) + assert table.schema._get_column_index_by_name("col1") == 0 and table.schema._get_column_index_by_name("col2") == 1 diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py b/tests/safeds/data/tabular/typing/_schema/test_get_column_type.py similarity index 100% rename from tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py rename to tests/safeds/data/tabular/typing/_schema/test_get_column_type.py diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_has_column.py b/tests/safeds/data/tabular/typing/_schema/test_has_column.py similarity index 100% rename from tests/safeds/data/tabular/typing/_table_schema/test_has_column.py rename to tests/safeds/data/tabular/typing/_schema/test_has_column.py diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py b/tests/safeds/data/tabular/typing/_schema/test_table_equals.py similarity index 80% rename from tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py rename to tests/safeds/data/tabular/typing/_schema/test_table_equals.py index 81f87955e..9688e5aaf 100644 --- a/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py +++ b/tests/safeds/data/tabular/typing/_schema/test_table_equals.py @@ -1,11 +1,11 @@ from safeds.data.tabular.containers import Table -from safeds.data.tabular.typing import Integer, RealNumber, TableSchema +from safeds.data.tabular.typing import Integer, RealNumber, Schema from tests.helpers import resolve_resource_path def test_table_equals_valid() -> None: table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) - schema_expected = TableSchema( + schema_expected = Schema( { "A": Integer(), "B": Integer(), @@ -17,7 +17,7 @@ def test_table_equals_valid() -> None: def test_table_equals_invalid() -> None: table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) - schema_not_expected = TableSchema( + schema_not_expected = Schema( { "A": RealNumber(), "C": Integer(),