Skip to content
Snippets Groups Projects
Commit 1229cea5 authored by David Schäfer's avatar David Schäfer
Browse files

added option to change History.squeeze behavior

parent 0671a6a7
No related branches found
No related tags found
1 merge request!826added option to change History.squeeze behavior
...@@ -19,6 +19,7 @@ SPDX-License-Identifier: GPL-3.0-or-later ...@@ -19,6 +19,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
- `plot`: added `yscope` keyword - `plot`: added `yscope` keyword
- `setFlags`: function to replace `flagManual` - `setFlags`: function to replace `flagManual`
- `flagUniLOF`: added defaultly applied correction to mitigate phenomenon of overflagging at relatively steep data value slopes. (parameter `slope_correct`). - `flagUniLOF`: added defaultly applied correction to mitigate phenomenon of overflagging at relatively steep data value slopes. (parameter `slope_correct`).
- `History`: added option to change aggregation behavior
### Changed ### Changed
### Removed ### Removed
### Fixed ### Fixed
......
...@@ -15,6 +15,13 @@ from pandas.api.types import is_float_dtype ...@@ -15,6 +15,13 @@ from pandas.api.types import is_float_dtype
from saqc import UNFLAGGED from saqc import UNFLAGGED
AGGRGEGATIONS = {
"last": lambda x: x.ffill(axis=1).iloc[:, -1],
"max": lambda x: x.max(axis=1),
"min": lambda x: x.min(axis=1),
}
AGGREGATION = "last"
class History: class History:
""" """
...@@ -306,7 +313,7 @@ class History: ...@@ -306,7 +313,7 @@ class History:
if hist.empty: if hist.empty:
result = pd.Series(data=np.nan, index=self._hist.index, dtype=float) result = pd.Series(data=np.nan, index=self._hist.index, dtype=float)
else: else:
result = hist.ffill(axis=1).iloc[:, -1] result = AGGRGEGATIONS[AGGREGATION](hist)
if not raw: if not raw:
result = result.fillna(UNFLAGGED) result = result.fillna(UNFLAGGED)
result.name = None result.name = None
......
...@@ -9,7 +9,7 @@ import pandas as pd ...@@ -9,7 +9,7 @@ import pandas as pd
import pytest import pytest
from pandas.api.types import is_categorical_dtype, is_float_dtype from pandas.api.types import is_categorical_dtype, is_float_dtype
from saqc.core.history import History, createHistoryFromData from saqc.core.history import AGGREGATION, History, createHistoryFromData
from tests.common import dummyHistory from tests.common import dummyHistory
# see #GH143 combined backtrack # see #GH143 combined backtrack
...@@ -240,3 +240,25 @@ def test_append_force(__hist, s, max_val): ...@@ -240,3 +240,25 @@ def test_append_force(__hist, s, max_val):
hist.append(s) hist.append(s)
check_invariants(hist) check_invariants(hist)
assert all(hist.squeeze() == max_val) assert all(hist.squeeze() == max_val)
@pytest.mark.parametrize(
"col, expected",
[
(pd.Series(0, index=range(6), dtype=float), {"last": 0, "min": 0, "max": 0}),
(pd.Series(1, index=range(6), dtype=float), {"last": 1, "min": 0, "max": 1}),
(pd.Series(6, index=range(6), dtype=float), {"last": 6, "min": 0, "max": 6}),
(pd.Series(4, index=range(6), dtype=float), {"last": 4, "min": 0, "max": 6}),
],
)
def test_aggregations(col, expected, hist=History(index=pd.Index(range(6)))):
import saqc.core.history
hist.append(col)
check_invariants(hist)
for aggregation in ["last", "min", "max"]:
saqc.core.history.AGGREGATION = aggregation
assert (hist.squeeze() == expected[aggregation]).all()
# reset to not disturb the other tests...
saqc.core.history.AGGREGATION = "last"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment