Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Column#transform #270

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d8f44dd
worked on transform column
patrikguempel May 5, 2023
22b5895
Added Column#transform()
patrikguempel May 5, 2023
80157b0
Merge branch 'main' of https://github.com/Safe-DS/Stdlib into 255-for…
patrikguempel May 5, 2023
57235ae
add type signature to transformer
patrikguempel May 5, 2023
7bf7b50
fixed code style
patrikguempel May 5, 2023
0e5964d
fixed code style
patrikguempel May 5, 2023
f2d4203
style: apply automated linter fixes
megalinter-bot May 5, 2023
4c79e71
style: apply automated linter fixes
megalinter-bot May 5, 2023
73d360e
Merge branch 'main' into 255-forward-pandas-apply-function-to-modify-…
lars-reimann May 5, 2023
0f8ec0d
style: move location of method
lars-reimann May 5, 2023
eafb862
Update src/safeds/data/tabular/containers/_column.py
patrikguempel May 5, 2023
b20f7f2
Update src/safeds/data/tabular/containers/_column.py
patrikguempel May 5, 2023
adf94df
some fixes and docs update
patrikguempel May 5, 2023
830a5dc
Merge branch 'main' into 255-forward-pandas-apply-function-to-modify-…
patrikguempel May 5, 2023
ff8ae5e
style: apply automated linter fixes
megalinter-bot May 5, 2023
55d37db
fixed example and improved test
patrikguempel May 5, 2023
7f92345
Merge remote-tracking branch 'origin/255-forward-pandas-apply-functio…
patrikguempel May 5, 2023
48ad9b5
Merge branch 'main' into 255-forward-pandas-apply-function-to-modify-…
patrikguempel May 5, 2023
fde0649
fixed test method sigantures
patrikguempel May 5, 2023
1b2d580
Merge remote-tracking branch 'origin/255-forward-pandas-apply-functio…
patrikguempel May 5, 2023
be37786
style: apply automated linter fixes
megalinter-bot May 5, 2023
5db22ad
chore: minor changes
lars-reimann May 6, 2023
726cbea
test: original column is not changed
lars-reimann May 6, 2023
6225317
style: apply automated linter fixes
megalinter-bot May 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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