Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/123-check-that-methods-of-table-…
Browse files Browse the repository at this point in the history
…can-handle-an-empty-table' into 123-check-that-methods-of-table-can-handle-an-empty-table
  • Loading branch information
patrikguempel committed Jun 6, 2023
2 parents 23b16f7 + 5142bdb commit f9d7c46
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 50 deletions.
12 changes: 12 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## [0.13.0](https://github.com/Safe-DS/Stdlib/compare/v0.12.0...v0.13.0) (2023-06-01)


### Features

* add `Choice` class for possible values of hyperparameter ([#325](https://github.com/Safe-DS/Stdlib/issues/325)) ([d511c3e](https://github.com/Safe-DS/Stdlib/commit/d511c3eed779e64fc53499e7c2eb2e8292955645)), closes [#264](https://github.com/Safe-DS/Stdlib/issues/264)
* Add `RangeScaler` transformer ([#310](https://github.com/Safe-DS/Stdlib/issues/310)) ([f687840](https://github.com/Safe-DS/Stdlib/commit/f68784057afe20d5450e9eb875fce1a07fb5fa77)), closes [#141](https://github.com/Safe-DS/Stdlib/issues/141)
* Add methods that tell which columns would be affected by a transformer ([#304](https://github.com/Safe-DS/Stdlib/issues/304)) ([3933b45](https://github.com/Safe-DS/Stdlib/commit/3933b458042f524d337f41d0ffa3aa4da16f5a2e)), closes [#190](https://github.com/Safe-DS/Stdlib/issues/190)
* Getters for hyperparameters of Regression and Classification models ([#306](https://github.com/Safe-DS/Stdlib/issues/306)) ([5c7a662](https://github.com/Safe-DS/Stdlib/commit/5c7a6623cd47f7c6cc25d2cd02179ff5b1a520d9)), closes [#260](https://github.com/Safe-DS/Stdlib/issues/260)
* improve error handling of table ([#308](https://github.com/Safe-DS/Stdlib/issues/308)) ([ef87cc4](https://github.com/Safe-DS/Stdlib/commit/ef87cc4d7f62fd0830688f8535e53ad7e2329457)), closes [#147](https://github.com/Safe-DS/Stdlib/issues/147)
* Remove warnings thrown in new `Transformer` methods ([#324](https://github.com/Safe-DS/Stdlib/issues/324)) ([ca046c4](https://github.com/Safe-DS/Stdlib/commit/ca046c40217bebcc05af98129d4e194c0509c9bb)), closes [#323](https://github.com/Safe-DS/Stdlib/issues/323)

## [0.12.0](https://github.com/Safe-DS/Stdlib/compare/v0.11.0...v0.12.0) (2023-05-11)


Expand Down
92 changes: 46 additions & 46 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "safe-ds"
version = "0.12.0"
version = "0.13.0"
description = "A user-friendly library for Data Science in Python."
authors = ["Lars Reimann <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -29,8 +29,8 @@ pytest-cov = "^4.0.0"
[tool.poetry.group.docs.dependencies]
jupyter = "^1.0.0"
mkdocs = "^1.4.2"
mkdocstrings = ">=0.20,<0.22"
mkdocstrings-python = ">=0.8.3,<0.10.0"
mkdocstrings = ">=0.20,<0.23"
mkdocstrings-python = ">=0.8.3,<1.2.0"
mkdocs-autorefs = "^0.4.1"
mkdocs-gen-files = ">=0.4,<0.6"
mkdocs-glightbox = "^0.3.1"
Expand Down
28 changes: 27 additions & 1 deletion src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, TypeVar

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -795,6 +795,32 @@ def filter_rows(self, query: Callable[[Row], bool]) -> Table:
result_table = self.from_rows(rows)
return result_table

_T = TypeVar("_T")

def group_by(self, key_selector: Callable[[Row], _T]) -> dict[_T, Table]:
"""
Return a dictionary with the output tables as values and the keys from the key_selector.
This table is not modified.
Parameters
----------
key_selector : Callable[[Row], _T]
A Callable that is applied to all rows and returns the key of the group.
Returns
-------
dictionary : dict
A dictionary containing the new tables as values and the selected keys as keys.
"""
dictionary: dict[Table._T, Table] = {}
for row in self.to_rows():
if key_selector(row) in dictionary:
dictionary[key_selector(row)] = dictionary[key_selector(row)].add_row(row)
else:
dictionary[key_selector(row)] = Table.from_rows([row])
return dictionary

def keep_only_columns(self, column_names: list[str]) -> Table:
"""
Return a table with only the given column(s).
Expand Down
35 changes: 35 additions & 0 deletions tests/safeds/data/tabular/containers/_table/test_group_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections.abc import Callable

import pytest
from safeds.data.tabular.containers import Table


@pytest.mark.parametrize(
("table", "selector", "expected"),
[
(
Table({"col1": [1, 1, 2, 2, 3], "col2": ["a", "b", "c", "d", "e"]}),
lambda row: row["col1"],
{
1: Table({"col1": [1, 1], "col2": ["a", "b"]}),
2: Table({"col1": [2, 2], "col2": ["c", "d"]}),
3: Table({"col1": [3], "col2": ["e"]}),
},
),
(
Table({"col1": [1, 1, 2, 2, 3], "col2": ["a", "b", "c", "d", 2]}),
lambda row: row["col1"],
{
1: Table({"col1": [1, 1], "col2": ["a", "b"]}),
2: Table({"col1": [2, 2], "col2": ["c", "d"]}),
3: Table({"col1": [3], "col2": [2]}),
},
),
(Table(), lambda row: row["col1"], {}),
(Table({"col1": [], "col2": []}), lambda row: row["col1"], {}),
],
ids=["select by row1", "different types in column", "empty table", "table with no rows"],
)
def test_group_by(table: Table, selector: Callable, expected: dict) -> None:
out = table.group_by(selector)
assert out == expected

0 comments on commit f9d7c46

Please sign in to comment.