From 555f5251aa9f6b0a7480ce183f186254d4b05d8e Mon Sep 17 00:00:00 2001
From: David Schaefer <david.schaefer@ufz.de>
Date: Thu, 1 Feb 2024 01:07:32 +0100
Subject: [PATCH] working on - multi field, single target generates to many
 empty history columns

---
 saqc/core/history.py          |  3 +++
 saqc/funcs/flagtools.py       | 10 ++++++++--
 saqc/lib/tools.py             |  4 +++-
 tests/funcs/test_flagtools.py | 23 +++++++++++++++++++++++
 4 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/saqc/core/history.py b/saqc/core/history.py
index 3391697c9..23e6fa59a 100644
--- a/saqc/core/history.py
+++ b/saqc/core/history.py
@@ -435,6 +435,9 @@ class History:
         new._meta = copyfunc(self._meta)
         return new
 
+    def equals(self, other: History) -> bool:
+        return self._hist.equals(other._hist) and self.meta == other.meta
+
     def __copy__(self):
         return self.copy(deep=False)
 
diff --git a/saqc/funcs/flagtools.py b/saqc/funcs/flagtools.py
index 3373855dd..b8c1fbc10 100644
--- a/saqc/funcs/flagtools.py
+++ b/saqc/funcs/flagtools.py
@@ -17,6 +17,7 @@ from typing_extensions import Literal
 
 from saqc import BAD, FILTER_ALL, UNFLAGGED
 from saqc.core import DictOfSeries, flagging, register
+from saqc.core.flags import Flags
 from saqc.core.history import History
 from saqc.lib.checking import validateChoice, validateWindow
 from saqc.lib.tools import initializeTargets, isflagged, isunflagged, multivariateParameters, toSequence
@@ -355,6 +356,10 @@ class FlagtoolsMixin:
 
         for field, target in zip(fields, targets):
 
+            if target not in self._data:
+                self._data[target] = pd.Series(np.nan, index=self._data[field].index)
+                self._flags._data[target] = History(self._data[target].index)
+
             history = self._flags.history[field]
             # append a dummy column
             meta = {
@@ -380,8 +385,9 @@ class FlagtoolsMixin:
             else:
                 flags = pd.Series(np.nan, index=history.index, dtype=float)
 
-                history.append(flags, meta)
-                self._flags.history[target].append(history)
+            history.append(flags, meta)
+            self._flags.history[target].append(history)
+            import ipdb; ipdb.set_trace()
 
 
         return self
diff --git a/saqc/lib/tools.py b/saqc/lib/tools.py
index 9132012a2..ad55cfb52 100644
--- a/saqc/lib/tools.py
+++ b/saqc/lib/tools.py
@@ -654,7 +654,9 @@ def joinExt(sep: str, iterable: Iterable[str], last_sep: str | None = None) -> s
     return f"{sep.join(iterable[:-1])}{last_sep}{iterable[-1]}"
 
 
-def multivariateParameters(field: str | list[str], target: str | list[str] | None = None) -> tuple[list[str], list[str]]:
+def multivariateParameters(
+    field: str | list[str], target: str | list[str] | None = None
+) -> tuple[list[str], list[str]]:
     fields = toSequence(field)
     targets = fields if target is None else toSequence(target)
 
diff --git a/tests/funcs/test_flagtools.py b/tests/funcs/test_flagtools.py
index b5de1df5b..0103314dd 100644
--- a/tests/funcs/test_flagtools.py
+++ b/tests/funcs/test_flagtools.py
@@ -175,3 +175,26 @@ def test__groupOperation(field, target, expected, copy):
         fields = toSequence(itertools.chain.from_iterable(field))
         for f, t in zip(fields, targets):
             assert (result._data[f] == result._data[t]).all(axis=None)
+
+
+def test_transferFlags():
+    qc = SaQC(
+        data=pd.DataFrame(
+            {"x": [0, 1, 2, 3], "y": [0, 11, 22, 33], "z": [0, 111, 222, 333]}
+        ),
+        flags=pd.DataFrame({"x": [B, U, U, B], "y": [B, B, U, U], "z": [B, B, U, B]}),
+    )
+
+    # qc1 = qc.transferFlags("x", target="a")
+    # assert qc1._history["a"].equals(qc1._history["x"])
+
+    # qc2 = qc.transferFlags(["x", "y"], target=["a", "b"])
+    # assert qc2._history["a"].equals(qc2._history["x"])
+    # assert qc2._history["b"].equals(qc2._history["y"])
+
+    qc3 = qc.transferFlags(["x", "y", "z"], target="a")
+    import ipdb; ipdb.set_trace()
+    assert qc3._history["a"].equals(qc2._history["x"].append(qc2._history["y"]).append(qc2._history["z"]))
+
+
+
-- 
GitLab