From ec539eb6685d99183a35a138d1f345aaf6ae4c78 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Fri, 24 Mar 2023 22:38:18 +0100 Subject: [PATCH] feat: change the name of a `Column` (#76) ### Summary of Changes New method on `Column` that returns a new column with a new name. --- src/safeds/data/tabular/containers/_column.py | 16 ++++++++++++++++ .../tabular/containers/_column/test_rename.py | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/safeds/data/tabular/containers/_column/test_rename.py diff --git a/src/safeds/data/tabular/containers/_column.py b/src/safeds/data/tabular/containers/_column.py index f4c30bf59..1eb3b9d69 100644 --- a/src/safeds/data/tabular/containers/_column.py +++ b/src/safeds/data/tabular/containers/_column.py @@ -121,6 +121,22 @@ def _count_missing_values(self) -> int: """ return self._data.isna().sum() + def rename(self, new_name: str) -> Column: + """ + Return a new column with a new name. + + Parameters + ---------- + new_name : str + The new name of the column. + + Returns + ------- + column : Column + A new column with the new name. + """ + return Column(self._data, new_name) + def all(self, predicate: Callable[[Any], bool]) -> bool: """ Check if all values have a given property. diff --git a/tests/safeds/data/tabular/containers/_column/test_rename.py b/tests/safeds/data/tabular/containers/_column/test_rename.py new file mode 100644 index 000000000..e6a3d44ed --- /dev/null +++ b/tests/safeds/data/tabular/containers/_column/test_rename.py @@ -0,0 +1,13 @@ +from safeds.data.tabular.containers import Column + + +def test_should_return_new_column_with_new_name() -> None: + column = Column([1, 2, 3], "A") + new_column = column.rename("B") + assert new_column.name == "B" + + +def test_should_not_change_name_of_original_column() -> None: + column = Column([1, 2, 3], "A") + column.rename("B") + assert column.name == "A"