Skip to content

Commit

Permalink
feat: Added Column#transform (#270)
Browse files Browse the repository at this point in the history
Closes #255 .

### Summary of Changes

Users are now able to call Column#transform to apply a function to every
data point within the column

Co-authored-by: [email protected]

---------

Co-authored-by: megalinter-bot <[email protected]>
Co-authored-by: Lars Reimann <[email protected]>
  • Loading branch information
3 people authored May 6, 2023
1 parent 626f26b commit 40fb756
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from collections.abc import Callable, Iterator

T = TypeVar("T")
R = TypeVar("R")


class Column(Sequence[T]):
Expand Down Expand Up @@ -300,6 +301,8 @@ def rename(self, new_name: str) -> Column:
"""
Return a new column with a new name.
This column is not modified.
Parameters
----------
new_name : str
Expand All @@ -312,6 +315,30 @@ def rename(self, new_name: str) -> Column:
"""
return Column._from_pandas_series(self._data.rename(new_name), self._type)

def transform(self, transformer: Callable[[T], R]) -> Column[R]:
"""
Apply a transform method to every data point.
This column is not modified.
Parameters
----------
transformer : Callable[[T], R]
Function that will be applied to all data points.
Returns
-------
transformed_column: Column
The transformed column.
Examples
--------
>>> from safeds.data.tabular.containers import Column
>>> price = Column("price", [4.99, 5.99, 2.49])
>>> sale = price.transform(lambda amount: amount * 0.8)
"""
return Column(self.name, self._data.apply(transformer, convert_dtype=True))

# ------------------------------------------------------------------------------------------------------------------
# Statistics
# ------------------------------------------------------------------------------------------------------------------
Expand Down
29 changes: 29 additions & 0 deletions tests/safeds/data/tabular/containers/_column/test_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from safeds.data.tabular.containers import Column


@pytest.mark.parametrize(
("column", "expected"),
[
(Column("test", []), Column("test", [])),
(Column("test", [1, 2]), Column("test", [2, 3])),
(Column("test", [-0.5, 0, 4]), Column("test", [0.5, 1, 5])),
],
ids=["empty", "integers", "floats"],
)
def test_should_transform_column(column: Column, expected: Column) -> None:
assert column.transform(lambda it: it + 1) == expected


@pytest.mark.parametrize(
("column", "original"),
[
(Column("test", []), Column("test", [])),
(Column("test", [1, 2]), Column("test", [1, 2])),
(Column("test", [-0.5, 0, 4]), Column("test", [-0.5, 0, 4])),
],
ids=["empty", "integers", "floats"],
)
def test_should_not_change_original_column(column: Column, original: Column) -> None:
column.transform(lambda it: it + 1)
assert column == original

0 comments on commit 40fb756

Please sign in to comment.