Skip to content

Commit 565ca55

Browse files
committed
added missing values for plot
1 parent 8dd5b87 commit 565ca55

13 files changed

+48
-2
lines changed

src/safeds/data/tabular/plotting/_table_plotter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ def moving_average_plot(self, x_name: str, y_name: str, window_size: int) -> Ima
392392
import polars as pl
393393

394394
_plot_validation(self._table, x_name, [y_name])
395+
396+
395397
# Calculate the moving average
396398
mean_col = pl.col(y_name).mean().alias(y_name)
397399
grouped = self._table._lazy_frame.sort(x_name).group_by(x_name).agg(mean_col).collect()
@@ -427,6 +429,10 @@ def _plot_validation(table: Table, x_name: str, y_names: list[str]) -> None:
427429
_check_columns_exist(table, y_names)
428430
y_names.remove(x_name)
429431
_check_columns_are_numeric(table, y_names)
432+
y_names.append(x_name)
430433

431434
if not table.get_column(x_name).is_numeric and not table.get_column(x_name).is_temporal:
432435
raise ColumnTypeError(x_name)
436+
for name in y_names:
437+
if table.get_column(name).missing_value_count() >= 1:
438+
raise ValueError("for plotting there should be no missing values")

tests/safeds/data/tabular/plotting/test_moving_average_plot.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from safeds.data.tabular.containers import Table
55
from syrupy import SnapshotAssertion
66

7+
from safeds.exceptions import ColumnNotFoundError, ColumnTypeError
8+
79

810
@pytest.mark.parametrize(
911
("table", "x_name", "y_name", "window_size"),
1012
[
1113
(Table({"A": [1, 2, 3], "B": [2, 4, 7]}), "A", "B", 2),
12-
# (Table({"A": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], "B": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]}), "A", "B", 2),
14+
(Table({"A": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], "B": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]}), "A", "B", 2),
1315
(
1416
Table(
1517
{
@@ -45,7 +47,7 @@
4547
2,
4648
),
4749
],
48-
ids=["numerical", "date grouped", "date"],
50+
ids=["numerical","numerical grouped", "date grouped", "date"],
4951
)
5052
def test_should_match_snapshot(
5153
table: Table,
@@ -56,3 +58,41 @@ def test_should_match_snapshot(
5658
) -> None:
5759
line_plot = table.plot.moving_average_plot(x_name, y_name, window_size)
5860
assert line_plot == snapshot_png_image
61+
62+
63+
@pytest.mark.parametrize(
64+
("x", "y"),
65+
[
66+
("C", "A"),
67+
("A", "C"),
68+
("C", "D"),
69+
],
70+
ids=["x column", "y column", "x and y column"],
71+
)
72+
def test_should_raise_if_column_does_not_exist_error_message(x: str, y: str) -> None:
73+
table = Table({"A": [1, 2, 3], "B": [2, 4, 7]})
74+
with pytest.raises(ColumnNotFoundError):
75+
table.plot.moving_average_plot(x, y, window_size=2)
76+
77+
78+
79+
80+
@pytest.mark.parametrize(
81+
("table"),
82+
[
83+
(Table({"A": [1, 2, 3], "B": ["2", 4, 7]})),
84+
(Table({"A": ["1", 2, 3], "B": [2, 4, 7]})),
85+
],
86+
ids=["x column", "y column"],
87+
)
88+
89+
def test_should_raise_if_column_is_not_numerical(table: Table) -> None:
90+
print(table)
91+
with pytest.raises(ColumnTypeError):
92+
table.plot.moving_average_plot("A", "B", window_size=2)
93+
94+
95+
def test_should_raise_if_column_has_missing_value() -> None:
96+
table = Table({"A": [None, 2, 3], "B": [2, 4, 7]})
97+
with pytest.raises(ValueError):
98+
table.plot.moving_average_plot("A", "B", window_size=2)

0 commit comments

Comments
 (0)