diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index fed28feac..a3fed3a4f 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -18,7 +18,6 @@ DuplicateColumnNameError, IndexOutOfBoundsError, MissingDataError, - MissingSchemaError, NonNumericColumnError, SchemaMismatchError, UnknownColumnNameError, @@ -193,14 +192,10 @@ def from_rows(rows: list[Row]) -> Table: def __init__(self, data: Iterable, schema: Optional[TableSchema] = None): self._data: pd.Dataframe = data if isinstance(data, pd.DataFrame) else pd.DataFrame(data) - if schema is None: - if self.count_columns() == 0: - raise MissingSchemaError() - self._schema: TableSchema = TableSchema._from_dataframe(self._data) - else: - self._schema = schema - if self._data.empty: - self._data = pd.DataFrame(columns=self._schema.get_column_names()) + self._schema: TableSchema = TableSchema._from_dataframe(self._data) if schema is None else schema + + if self._data.empty: + self._data = pd.DataFrame(columns=self._schema.get_column_names()) self._data = self._data.reset_index(drop=True) self._data.columns = list(range(self.count_columns())) diff --git a/tests/safeds/data/tabular/containers/_table/test_table.py b/tests/safeds/data/tabular/containers/_table/test_table.py index 33a0c24c6..7410871b7 100644 --- a/tests/safeds/data/tabular/containers/_table/test_table.py +++ b/tests/safeds/data/tabular/containers/_table/test_table.py @@ -4,10 +4,13 @@ def test_create_empty_table() -> None: - table = Table( - [], TableSchema({"col1": ColumnType.from_numpy_dtype(np.dtype(float))}) - ) + table = Table([], TableSchema({"col1": ColumnType.from_numpy_dtype(np.dtype(float))})) col = table.get_column("col1") assert col.count() == 0 assert isinstance(col.type, type(ColumnType.from_numpy_dtype(np.dtype(float)))) assert col.name == "col1" + + +def test_create_empty_table_without_schema() -> None: + table = Table([]) + assert table.schema == TableSchema({})