From 9b8414df99eb547994e750f37d4cd7aa17051793 Mon Sep 17 00:00:00 2001 From: Bert Palm <bert.palm@ufz.de> Date: Fri, 14 Feb 2020 15:37:56 +0100 Subject: [PATCH] added reraise --- dios/dios.py | 57 ++++++++++++++++++++++++++++++++++------------- dios/lib.py | 9 ++++++++ test/run_dios.py | 3 +++ test/test_dios.py | 25 +++++++++++++++++++++ 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/dios/dios.py b/dios/dios.py index 28eba26..2e3701a 100644 --- a/dios/dios.py +++ b/dios/dios.py @@ -1,6 +1,7 @@ from dios.lib import * from dios.options import * import pandas as pd +import numpy as np import operator as op from collections import OrderedDict @@ -56,7 +57,7 @@ class DictOfSeries: """ - def __init__(self, data=None, itype=None, columns=None, downcast_policy='lossless'): + def __init__(self, data=None, columns=None, itype=None, downcast_policy='lossless'): self._data = OrderedDict() @@ -67,7 +68,8 @@ class DictOfSeries: # May data was given, so we firstly set itype to MixedItype, then insert all data, # and check/cast the itype afterwards, otherwise __setitem_new() will set the itype, # which may prevent inserting series with other (higher) itypes. - self._itype = MixedItype + with reraise("param itype: "): + self._itype = get_itype(itype) policies = ['force', 'lossless', 'never'] if downcast_policy not in policies: @@ -400,18 +402,26 @@ class DictOfSeries: pd.set_option('display.max_rows', pd_max_rows) return s - def __bool__(self): - raise ValueError("The truth value of a DictionaryOfSeries is ambiguous. Use a.empty, a.any() or a.all().") + # def __bool__(self): + # raise ValueError("The truth value of a DictionaryOfSeries is ambiguous. Use a.empty, a.any() or a.all().") @property def empty(self): return all(self._data[c].empty for c in self._data) def all(self): - return all(all(self._data[c]) for c in self._data) + return pd.Series([all(self._data[c]) for c in self._data]) def any(self): - return any(any(self._data[c]) for c in self._data) + return pd.Series([any(self._data[c]) for c in self._data]) + + def aaall(self): + """absolute all all""" + return self.all().all() + + def aaany(self): + """absolute any any""" + return self.any().any() def __len__(self): return len(self._data) @@ -425,6 +435,9 @@ class DictOfSeries: def __iter__(self): yield from self._data + # def __array__(self): + # return [False, True] + def __delitem__(self, key): del self._data[key] @@ -466,15 +479,9 @@ class DictOfSeries: new[k] = op(self[k], other) return new - def __neg__(self): - return self.__op1__(op.neg) - - def __invert__(self): - return self.__op1__(op.inv) - - def __abs__(self): - return self.__op1__(op.abs) - + # ---------------------------------- + # comparators + # ---------------------------------- def __lt__(self, other): return self.__op2__(other, op.lt) @@ -493,6 +500,18 @@ class DictOfSeries: def __gt__(self, other): return self.__op2__(other, op.gt) + # ---------------------------------- + # arithmetical + # ---------------------------------- + + def __neg__(self): + # -dios + return self.__op1__(op.neg) + + def __abs__(self): + # abs(dios) + return self.__op1__(op.abs) + def __add__(self, other): return self.__op2__(other, op.add) @@ -514,6 +533,14 @@ class DictOfSeries: def __pow__(self, other): return self.__op2__(other, op.pow) + # ---------------------------------- + # bool stuff + # ---------------------------------- + + def __invert__(self): + # ~dios + return self.__op1__(op.inv) + def __and__(self, other): return self.__op2__(other, op.and_) diff --git a/dios/lib.py b/dios/lib.py index 38f5662..27df4f4 100644 --- a/dios/lib.py +++ b/dios/lib.py @@ -3,6 +3,15 @@ from dios.options import * import pandas as pd import warnings +import contextlib + + +@contextlib.contextmanager +def reraise(prefix="", postfix=""): + try: + yield + except Exception as e: + raise type(e)(prefix + str(e) + postfix) from e def _get_storage_class_values(cls): diff --git a/test/run_dios.py b/test/run_dios.py index 2ab7f58..14303e2 100644 --- a/test/run_dios.py +++ b/test/run_dios.py @@ -6,6 +6,9 @@ if __name__ == '__main__': # dios_options[Options.mixed_itype_policy] = 'error' dios = DictOfSeries(data=[234.54, 5, 5, 4, np.nan, 5, 4, 5]) + + print(all(dios == dios)) + dtser = pd.Series([2,4,4123,122,4], index=pd.date_range(freq='1d', periods=5, start='2000-01-01')) dios['b'] = dtser dios2 = dios.copy() diff --git a/test/test_dios.py b/test/test_dios.py index 0bc7ef0..b8c0b9f 100644 --- a/test/test_dios.py +++ b/test/test_dios.py @@ -56,3 +56,28 @@ def test_copy_copy_empty__copy__(dictofseries): dios[i][0] = 999999 assert dios[i][0] == shallow[i][0] assert dios[i][0] != deep[i][0] + + +def test__mul__(dictofseries): + a = dictofseries.copy() + for i in [1,2,50, 33.3, 0.1, 0]: + b = a * i + for c in b: + exp = a[c] * i + assert np.all(b[c] == exp) + assert b[c] is not exp + + +def test__eq__(dictofseries): + a = dictofseries.copy() + b = dictofseries.copy() + + assert a is not b + assert (a == b).aaall() + x = b['c0'] + x.iloc[5] = 5 + b['c0'] = x + assert (a != b).aaany() + # assert False + + -- GitLab