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

fix: now mode returns a list #396

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
8 changes: 4 additions & 4 deletions Runtime/safe-ds/safeds/data/tabular/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ def mode(self) -> Any:

Returns
-------
mode :
The mode value.
List :
Returns a list with the most common values.
"""
return self._column._data.mode()[0]
return self._column._data.mode().tolist()

def median(self) -> float:
"""
Expand Down Expand Up @@ -443,7 +443,7 @@ def stability(self) -> float:
if self._column._data.size == 0:
raise ColumnSizeError("> 0", "0")
return (
self._column._data.value_counts()[self._column.statistics.mode()]
self._column._data.value_counts()[self._column.statistics.mode()[0]]
/ self._column._data.count()
)

Expand Down
10 changes: 8 additions & 2 deletions Runtime/safe-ds/tests/data/tabular/_column/test_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
def test_mode_valid() -> None:
table = Table(pd.DataFrame(data={"col1": [1, 2, 3, 4, 3]}))
column = table.get_column("col1")
assert column.statistics.mode() == 3
assert column.statistics.mode() == [3]


def test_mode_valid_str() -> None:
table = Table(pd.DataFrame(data={"col1": ["1", "2", "3", "4", "3"]}))
column = table.get_column("col1")
assert column.statistics.mode() == "3"
assert column.statistics.mode() == ["3"]


def test_mode_valid_list() -> None:
table = Table(pd.DataFrame(data={"col1": ["1", "4", "3", "4", "3"]}))
column = table.get_column("col1")
assert column.statistics.mode() == ["3", "4"]
7 changes: 3 additions & 4 deletions Runtime/safe-ds/tests/data/tabular/_table/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_summary() -> None:
truth = Table(
pd.DataFrame(
data={
"": [
"metrics": [
"max",
"min",
"mean",
Expand All @@ -25,7 +25,7 @@ def test_summary() -> None:
"2",
"1",
str(4.0 / 3),
"1",
"[1]",
"1.0",
"4",
str(1.0 / 3),
Expand All @@ -38,7 +38,7 @@ def test_summary() -> None:
"-",
"-",
"-",
"a",
"['a', 'b', 'c']",
"-",
"-",
"-",
Expand All @@ -50,5 +50,4 @@ def test_summary() -> None:
}
)
)

assert truth == table.summary()
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def test_imputer_median() -> None:


def test_imputer_mode() -> None:
table = Table(pd.DataFrame(data={"col1": [np.nan, 2, 3, 4, 5]}))
table = Table(pd.DataFrame(data={"col1": [np.nan, 2, 2, 4, 5]}))
column = table.get_column("col1")
imp = Imputer(Imputer.Strategy.Mode())
new_table = imp.fit_transform(table)

assert new_table.get_column("col1")._data[0] == column.statistics.mode()
assert new_table.get_column("col1")._data[0] == column.statistics.mode()[0]


def test_imputer_constant() -> None:
Expand Down