Skip to content

Commit

Permalink
fix: avoid performance warning when accessing column_count (#949)
Browse files Browse the repository at this point in the history
### Summary of Changes

Avoid a potential `PerformanceWarning` raised by `polars` when accessing
`Table.column_count`.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored Nov 26, 2024
1 parent 3ac00ed commit c24b6d5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
14 changes: 3 additions & 11 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -390,13 +389,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:
Expand Down Expand Up @@ -612,8 +605,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:
"""
Expand All @@ -636,7 +628,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,
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/typing/_polars_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down

0 comments on commit c24b6d5

Please sign in to comment.