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

added reraise

parent b193c4ce
No related branches found
No related tags found
2 merge requests!2Develop,!1complete rework
from dios.lib import * from dios.lib import *
from dios.options import * from dios.options import *
import pandas as pd import pandas as pd
import numpy as np
import operator as op import operator as op
from collections import OrderedDict from collections import OrderedDict
...@@ -56,7 +57,7 @@ class DictOfSeries: ...@@ -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() self._data = OrderedDict()
...@@ -67,7 +68,8 @@ class DictOfSeries: ...@@ -67,7 +68,8 @@ class DictOfSeries:
# May data was given, so we firstly set itype to MixedItype, then insert all data, # 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, # and check/cast the itype afterwards, otherwise __setitem_new() will set the itype,
# which may prevent inserting series with other (higher) itypes. # 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'] policies = ['force', 'lossless', 'never']
if downcast_policy not in policies: if downcast_policy not in policies:
...@@ -400,18 +402,26 @@ class DictOfSeries: ...@@ -400,18 +402,26 @@ class DictOfSeries:
pd.set_option('display.max_rows', pd_max_rows) pd.set_option('display.max_rows', pd_max_rows)
return s return s
def __bool__(self): # def __bool__(self):
raise ValueError("The truth value of a DictionaryOfSeries is ambiguous. Use a.empty, a.any() or a.all().") # raise ValueError("The truth value of a DictionaryOfSeries is ambiguous. Use a.empty, a.any() or a.all().")
@property @property
def empty(self): def empty(self):
return all(self._data[c].empty for c in self._data) return all(self._data[c].empty for c in self._data)
def all(self): 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): 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): def __len__(self):
return len(self._data) return len(self._data)
...@@ -425,6 +435,9 @@ class DictOfSeries: ...@@ -425,6 +435,9 @@ class DictOfSeries:
def __iter__(self): def __iter__(self):
yield from self._data yield from self._data
# def __array__(self):
# return [False, True]
def __delitem__(self, key): def __delitem__(self, key):
del self._data[key] del self._data[key]
...@@ -466,15 +479,9 @@ class DictOfSeries: ...@@ -466,15 +479,9 @@ class DictOfSeries:
new[k] = op(self[k], other) new[k] = op(self[k], other)
return new return new
def __neg__(self): # ----------------------------------
return self.__op1__(op.neg) # comparators
# ----------------------------------
def __invert__(self):
return self.__op1__(op.inv)
def __abs__(self):
return self.__op1__(op.abs)
def __lt__(self, other): def __lt__(self, other):
return self.__op2__(other, op.lt) return self.__op2__(other, op.lt)
...@@ -493,6 +500,18 @@ class DictOfSeries: ...@@ -493,6 +500,18 @@ class DictOfSeries:
def __gt__(self, other): def __gt__(self, other):
return self.__op2__(other, op.gt) 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): def __add__(self, other):
return self.__op2__(other, op.add) return self.__op2__(other, op.add)
...@@ -514,6 +533,14 @@ class DictOfSeries: ...@@ -514,6 +533,14 @@ class DictOfSeries:
def __pow__(self, other): def __pow__(self, other):
return self.__op2__(other, op.pow) return self.__op2__(other, op.pow)
# ----------------------------------
# bool stuff
# ----------------------------------
def __invert__(self):
# ~dios
return self.__op1__(op.inv)
def __and__(self, other): def __and__(self, other):
return self.__op2__(other, op.and_) return self.__op2__(other, op.and_)
......
...@@ -3,6 +3,15 @@ from dios.options import * ...@@ -3,6 +3,15 @@ from dios.options import *
import pandas as pd import pandas as pd
import warnings 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): def _get_storage_class_values(cls):
......
...@@ -6,6 +6,9 @@ if __name__ == '__main__': ...@@ -6,6 +6,9 @@ if __name__ == '__main__':
# dios_options[Options.mixed_itype_policy] = 'error' # dios_options[Options.mixed_itype_policy] = 'error'
dios = DictOfSeries(data=[234.54, 5, 5, 4, np.nan, 5, 4, 5]) 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')) dtser = pd.Series([2,4,4123,122,4], index=pd.date_range(freq='1d', periods=5, start='2000-01-01'))
dios['b'] = dtser dios['b'] = dtser
dios2 = dios.copy() dios2 = dios.copy()
......
...@@ -56,3 +56,28 @@ def test_copy_copy_empty__copy__(dictofseries): ...@@ -56,3 +56,28 @@ def test_copy_copy_empty__copy__(dictofseries):
dios[i][0] = 999999 dios[i][0] = 999999
assert dios[i][0] == shallow[i][0] assert dios[i][0] == shallow[i][0]
assert dios[i][0] != deep[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
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