-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #197. ### Summary of Changes Added two new methods to `Table`: * `from_dict` can create a `Table` from a `dict` * `to_dict` can convert a `Table` to a `dict` --------- Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
459ab75
commit 2a5089e
Showing
62 changed files
with
676 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
import _pytest | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.data.tabular.exceptions import NonNumericColumnError | ||
|
||
|
||
def test_boxplot_complex() -> None: | ||
with pytest.raises(TypeError): # noqa: PT012 | ||
table = Table(pd.DataFrame(data={"A": [1, 2, complex(1, -2)]})) | ||
table = Table.from_dict({"A": [1, 2, complex(1, -2)]}) | ||
table.get_column("A").boxplot() | ||
|
||
|
||
def test_boxplot_non_numeric() -> None: | ||
table = Table(pd.DataFrame(data={"A": [1, 2, "A"]})) | ||
table = Table.from_dict({"A": [1, 2, "A"]}) | ||
with pytest.raises(NonNumericColumnError): | ||
table.get_column("A").boxplot() | ||
|
||
|
||
def test_boxplot_float(monkeypatch: _pytest.monkeypatch) -> None: | ||
monkeypatch.setattr(plt, "show", lambda: None) | ||
table = Table(pd.DataFrame(data={"A": [1, 2, 3.5]})) | ||
table = Table.from_dict({"A": [1, 2, 3.5]}) | ||
table.get_column("A").boxplot() | ||
|
||
|
||
def test_boxplot_int(monkeypatch: _pytest.monkeypatch) -> None: | ||
monkeypatch.setattr(plt, "show", lambda: None) | ||
table = Table(pd.DataFrame(data={"A": [1, 2, 3]})) | ||
table = Table.from_dict({"A": [1, 2, 3]}) | ||
table.get_column("A").boxplot() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
import pandas as pd | ||
from safeds.data.tabular.containers import Column | ||
|
||
|
||
def test_from_columns() -> None: | ||
column1 = Column("A", pd.Series([1, 4])) | ||
column2 = Column("B", pd.Series([2, 5])) | ||
column1 = Column("A", [1, 4]) | ||
column2 = Column("B", [2, 5]) | ||
|
||
assert column1._type == column2._type | ||
|
||
|
||
def test_from_columns_negative() -> None: | ||
column1 = Column("A", pd.Series([1, 4])) | ||
column2 = Column("B", pd.Series(["2", "5"])) | ||
column1 = Column("A", [1, 4]) | ||
column2 = Column("B", ["2", "5"]) | ||
|
||
assert column1._type != column2._type |
13 changes: 6 additions & 7 deletions
13
tests/safeds/data/tabular/containers/_column/test_column_properties.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import pandas as pd | ||
from safeds.data.tabular.containers import Column | ||
|
||
|
||
def test_column_property_all_positive() -> None: | ||
column = Column("col1", pd.Series([1, 1, 1])) | ||
column = Column("col1", [1, 1, 1]) | ||
assert column.all(lambda value: value == 1) | ||
|
||
|
||
def test_column_property_all_negative() -> None: | ||
column = Column("col1", pd.Series([1, 2, 1])) | ||
column = Column("col1", [1, 2, 1]) | ||
assert not column.all(lambda value: value == 1) | ||
|
||
|
||
def test_column_property_any_positive() -> None: | ||
column = Column("col1", pd.Series([1, 2, 1])) | ||
column = Column("col1", [1, 2, 1]) | ||
assert column.any(lambda value: value == 1) | ||
|
||
|
||
def test_column_property_any_negative() -> None: | ||
column = Column("col1", pd.Series([1, 2, 1])) | ||
column = Column("col1", [1, 2, 1]) | ||
assert not column.any(lambda value: value == 3) | ||
|
||
|
||
def test_column_property_none_positive() -> None: | ||
column = Column("col1", pd.Series([1, 2, 1])) | ||
column = Column("col1", [1, 2, 1]) | ||
assert column.none(lambda value: value == 3) | ||
|
||
|
||
def test_column_property_none_negative() -> None: | ||
column = Column("col1", pd.Series([1, 2, 1])) | ||
column = Column("col1", [1, 2, 1]) | ||
assert not column.none(lambda value: value == 1) |
13 changes: 6 additions & 7 deletions
13
tests/safeds/data/tabular/containers/_column/test_correlation_with.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
import pandas as pd | ||
import pytest | ||
from safeds.data.tabular.containers import Column | ||
from safeds.data.tabular.exceptions import ColumnLengthMismatchError, NonNumericColumnError | ||
|
||
|
||
def test_correlation_with() -> None: | ||
column1 = Column("A", pd.Series([1, 2, 3, 4])) | ||
column2 = Column("B", pd.Series([2, 3, 4, 5])) | ||
column1 = Column("A", [1, 2, 3, 4]) | ||
column2 = Column("B", [2, 3, 4, 5]) | ||
actual_corr = column1.correlation_with(column2) | ||
expected_corr = column1._data.corr(column2._data) | ||
assert actual_corr == expected_corr | ||
|
||
|
||
def test_correlation_with_raises_if_column_is_not_numeric() -> None: | ||
column1 = Column("A", pd.Series([1, 2, 3, 4])) | ||
column2 = Column("B", pd.Series(["a", "b", "c", "d"])) | ||
column1 = Column("A", [1, 2, 3, 4]) | ||
column2 = Column("B", ["a", "b", "c", "d"]) | ||
with pytest.raises(NonNumericColumnError): | ||
column1.correlation_with(column2) | ||
|
||
|
||
def test_correlation_with_raises_if_column_lengths_differ() -> None: | ||
column1 = Column("A", pd.Series([1, 2, 3, 4])) | ||
column2 = Column("B", pd.Series([2])) | ||
column1 = Column("A", [1, 2, 3, 4]) | ||
column2 = Column("B", [2]) | ||
with pytest.raises(ColumnLengthMismatchError): | ||
column1.correlation_with(column2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Column | ||
|
||
|
||
def test_count_valid() -> None: | ||
column = Column("col1", [1, 2, 3, 4, 5]) | ||
assert column.count() == 5 | ||
@pytest.mark.parametrize( | ||
("column", "expected"), | ||
[ | ||
(Column("col1", [1, 2, 3, 4, 5]), 5), | ||
], | ||
) | ||
def test_count_valid(column: Column, expected: int) -> None: | ||
assert column.count() == expected |
5 changes: 2 additions & 3 deletions
5
tests/safeds/data/tabular/containers/_column/test_count_null_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
tests/safeds/data/tabular/containers/_column/test_get_unique_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import typing | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from safeds.data.tabular.containers import Column | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("values", "unique_values"), | ||
[([1, 1, 2, 3], [1, 2, 3]), (["a", "b", "b", "c"], ["a", "b", "c"]), ([], [])], | ||
) | ||
def test_get_unique_values(values: list[typing.Any], unique_values: list[typing.Any]) -> None: | ||
def test_get_unique_values(values: list[Any], unique_values: list[Any]) -> None: | ||
column: Column = Column("", values) | ||
extracted_unique_values: list[typing.Any] = column.get_unique_values() | ||
extracted_unique_values = column.get_unique_values() | ||
|
||
assert extracted_unique_values == unique_values |
5 changes: 2 additions & 3 deletions
5
tests/safeds/data/tabular/containers/_column/test_get_value.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
tests/safeds/data/tabular/containers/_column/test_histogram.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import _pytest | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
from safeds.data.tabular.containers import Table | ||
|
||
|
||
def test_histogram(monkeypatch: _pytest.monkeypatch) -> None: | ||
monkeypatch.setattr(plt, "show", lambda: None) | ||
table = Table(pd.DataFrame(data={"A": [1, 2, 3]})) | ||
table = Table.from_dict({"A": [1, 2, 3]}) | ||
table.get_column("A").histogram() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import pandas as pd | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.data.tabular.exceptions import NonNumericColumnError | ||
|
||
|
||
def test_maximum_invalid() -> None: | ||
table = Table(pd.DataFrame(data={"col1": ["col1_1", 2]})) | ||
table = Table.from_dict({"col1": ["col1_1", 2]}) | ||
column = table.get_column("col1") | ||
with pytest.raises(NonNumericColumnError): | ||
column.maximum() | ||
|
||
|
||
def test_maximum_valid() -> None: | ||
table = Table(pd.DataFrame(data={"col1": [1, 2, 3, 4]})) | ||
table = Table.from_dict({"col1": [1, 2, 3, 4]}) | ||
column = table.get_column("col1") | ||
assert column.maximum() == 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import pandas as pd | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.data.tabular.exceptions import NonNumericColumnError | ||
|
||
|
||
def test_mean_invalid() -> None: | ||
table = Table(pd.DataFrame(data={"col1": ["col1_1", 2]})) | ||
table = Table.from_dict({"col1": ["col1_1", 2]}) | ||
column = table.get_column("col1") | ||
with pytest.raises(NonNumericColumnError): | ||
column.mean() | ||
|
||
|
||
def test_mean_valid() -> None: | ||
table = Table(pd.DataFrame(data={"col1": [1, 2, 3, 4]})) | ||
table = Table.from_dict({"col1": [1, 2, 3, 4]}) | ||
column = table.get_column("col1") | ||
assert column.mean() == 2.5 |
Oops, something went wrong.