Skip to content
Snippets Groups Projects
Commit c02b50ac authored by Bert Palm's avatar Bert Palm 🎇
Browse files

added reindex to History, and added tests for that; fixed old bug in test History

parent a09fd07f
No related branches found
No related tags found
1 merge request!231new Flagger and Stuff
......@@ -4,6 +4,7 @@ from __future__ import annotations
from typing import Tuple, Type
import pandas as pd
import numpy as np
from saqc.common import *
class History:
......@@ -149,7 +150,9 @@ class History:
-------
History
"""
# internal detail:
# Note:
# all following code must handle a passed empty series
# ensure continuous increasing columns
assert 0 <= pos <= len(self)
......@@ -191,11 +194,8 @@ class History:
"""
s = self._validate_value(value)
if s.empty:
raise ValueError('Cannot append empty pd.Series')
if not self.empty and not s.index.equals(self.index):
raise ValueError("Index must be equal to FH's index")
if len(self) > 0 and not s.index.equals(self.index):
raise ValueError("Index must be equal to FlagHistory index")
self._insert(value, pos=len(self), force=force)
return self
......@@ -279,6 +279,28 @@ class History:
"""
return self._constructor(hist=self, copy=deep)
def reindex(self, index: pd.Index, fill_value_last: float = UNFLAGGED) -> History:
"""
Reindex the History. Be careful this alters the past.
Parameters
----------
index : pd.Index
the index to reindex to.
fill_value_last : float, default 0
value to fill nan's (UNTOUCHED) in the last column. Defaults to 0 (UNFLAGGED).
Returns
-------
History
"""
self.hist = self.hist.reindex(index=index, copy=False, fill_value=np.nan)
self.mask = self.mask.reindex(index=index, copy=False, fill_value=False)
# Note: all following code must handle empty frames
self.hist.iloc[:, -1:] = self.hist.iloc[:, -1:].fillna(fill_value_last)
self.mask.iloc[:, -1:] = True
return self
def __copy__(self, deep: bool = True):
return self.copy(deep=deep)
......
......@@ -102,7 +102,11 @@ def check_invariants(hist):
# or the entire row is True
if not hist.empty:
idxmax = hist.mask.idxmax(axis=1)
print(f'idxmax: {idxmax}')
for row, col in idxmax.items():
# this is contra intuitive, it gets the positional (for iloc)
row = idxmax.index.get_loc(row)
assert all(hist.mask.iloc[row, :col] == False)
assert all(hist.mask.iloc[row, col:] == True)
......@@ -183,6 +187,41 @@ def test_copy(data):
assert shallow.mask is hist.mask
@pytest.mark.parametrize('data', data + [None])
def test_reindex_trivial_cases(data):
df = pd.DataFrame(data, dtype=float)
orig = History(hist=df)
# checks
for index in [df.index, pd.Index([])]:
hist = orig.copy()
ref = hist.reindex(index)
assert ref is hist # check if working inplace
check_invariants(hist)
@pytest.mark.parametrize('data', data + [None])
def test_reindex_missing_indicees(data):
df = pd.DataFrame(data, dtype=float)
hist = History(hist=df)
index = df.index[1:-1]
# checks
ref = hist.reindex(index)
assert ref is hist # check if working inplace
check_invariants(hist)
@pytest.mark.parametrize('data', data + [None])
def test_reindex_extra_indicees(data):
df = pd.DataFrame(data, dtype=float)
hist = History(hist=df)
index = df.index.append(pd.Index(range(len(df.index), len(df.index) + 5)))
# checks
ref = hist.reindex(index)
assert ref is hist # check if working inplace
check_invariants(hist)
@pytest.fixture(scope='module')
def __hist():
# this FH is filled by
......
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