From 9d64cdb847a5cf330246d8d1c95d04a7f52dbb27 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Tue, 26 Nov 2024 14:26:30 +0100 Subject: [PATCH 1/2] fix: avoid performance warning when accessing `column_count` --- src/safeds/data/tabular/containers/_table.py | 13 +++---------- src/safeds/data/tabular/typing/_polars_schema.py | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 67432558c..6c60bcd27 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -390,13 +390,7 @@ def column_count(self) -> int: >>> table.column_count 2 """ - import polars as pl - - try: - return self._lazy_frame.width - except (pl.NoDataError, pl.PolarsPanicError): - # Can happen for some operations on empty tables (e.g. https://github.com/pola-rs/polars/issues/16202) - return 0 + return len(self.column_names) @property def row_count(self) -> int: @@ -612,8 +606,7 @@ def get_column_type(self, name: str) -> DataType: >>> table.get_column_type("a") Int64 """ - _check_columns_exist(self, name) - return _PolarsDataType(self._lazy_frame.schema[name]) + return self.schema.get_column_type(name) def has_column(self, name: str) -> bool: """ @@ -636,7 +629,7 @@ def has_column(self, name: str) -> bool: >>> table.has_column("a") True """ - return name in self.column_names + return self.schema.has_column(name) def remove_columns( self, diff --git a/src/safeds/data/tabular/typing/_polars_schema.py b/src/safeds/data/tabular/typing/_polars_schema.py index c5023d8bf..5c619064e 100644 --- a/src/safeds/data/tabular/typing/_polars_schema.py +++ b/src/safeds/data/tabular/typing/_polars_schema.py @@ -73,7 +73,7 @@ def column_names(self) -> list[str]: # ------------------------------------------------------------------------------------------------------------------ def get_column_type(self, name: str) -> DataType: - _check_columns_exist(self, [name]) + _check_columns_exist(self, name) return _PolarsDataType(self._schema[name]) From 069231233b009ef8fd520206a940f4afda16b2d6 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:31:03 +0000 Subject: [PATCH 2/2] style: apply automated linter fixes --- src/safeds/data/tabular/containers/_table.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index 6c60bcd27..fc32f9cc7 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -9,7 +9,6 @@ from safeds._validation import _check_bounds, _check_columns_exist, _ClosedBound, _normalize_and_check_file_path from safeds._validation._check_columns_dont_exist import _check_columns_dont_exist from safeds.data.tabular.plotting import TablePlotter -from safeds.data.tabular.typing._polars_data_type import _PolarsDataType from safeds.data.tabular.typing._polars_schema import _PolarsSchema from safeds.exceptions import ( ColumnLengthMismatchError,