From 498999f50d9f48609043f87f45ec9383ace3afe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Severin=20Paul=20H=C3=B6fer?= <84280965+zzril@users.noreply.github.com> Date: Fri, 9 Jun 2023 09:59:54 +0200 Subject: [PATCH] feat: Explicitly throw `UnknownColumnNameError` in `TaggedTable._from_table` (#334) Closes #333. ### Summary of Changes Explicitly throw `UnknownColumnNameError` in `TaggedTable._from_table` for a more readable stacktrace. Document when this exception is thrown. Co-authored-by: Alexander <47296670+Marsmaennchen221@users.noreply.github.com> --- src/safeds/data/tabular/containers/_tagged_table.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/safeds/data/tabular/containers/_tagged_table.py b/src/safeds/data/tabular/containers/_tagged_table.py index d06fa154b..67641cc1e 100644 --- a/src/safeds/data/tabular/containers/_tagged_table.py +++ b/src/safeds/data/tabular/containers/_tagged_table.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING from safeds.data.tabular.containers import Column, Table +from safeds.exceptions import UnknownColumnNameError if TYPE_CHECKING: from collections.abc import Mapping, Sequence @@ -67,6 +68,8 @@ def _from_table( Raises ------ + UnknownColumnNameError + If target_name matches none of the column names. ValueError If the target column is also a feature column. ValueError @@ -78,11 +81,13 @@ def _from_table( >>> table = Table({"col1": ["a", "b", "c", "a"], "col2": [1, 2, 3, 4]}) >>> tagged_table = TaggedTable._from_table(table, "col2", ["col1"]) """ + if target_name not in table.column_names: + raise UnknownColumnNameError([target_name]) + # If no feature names are specified, use all columns except the target column if feature_names is None: feature_names = table.column_names - if target_name in feature_names: - feature_names.remove(target_name) + feature_names.remove(target_name) # Validate inputs if target_name in feature_names: