From 7fb7eb6ac94336f5905cd8b7515fe8cba1b4154f Mon Sep 17 00:00:00 2001 From: Bert Palm <bert.palm@ufz.de> Date: Thu, 13 Feb 2020 02:01:05 +0100 Subject: [PATCH] fixed bugs and minor animals --- dios/dios.py | 32 ++++++++++++++++++++------------ dios/itypes.py | 16 +++++++++++----- tests/run_dios.py | 10 +++++----- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/dios/dios.py b/dios/dios.py index 79218f4..965def1 100644 --- a/dios/dios.py +++ b/dios/dios.py @@ -54,7 +54,7 @@ class DictOfSeries: """ - def __init__(self, data=None, itype=None, columns=None): + def __init__(self, data=None, itype=MixedItype, columns=None): self._data = OrderedDict() @@ -67,10 +67,12 @@ class DictOfSeries: # which may prevent inserting series with other (higher) itypes. self._itype = MixedItype - self.__init_insert_data__(data) + if data is not None: + self.__init_insert_data__(data) # we use the columns.setter to make all necessary checks - self.columns = columns + if columns is not None: + self.columns = columns # infer the itype by the data inferred_itype = self.__find_least_common_itype() @@ -78,12 +80,14 @@ class DictOfSeries: # We use the itype.setter to make all checks. If the given itype was of a lower type # than the inferred itype, a cast is tried on every series. - self.itype = itype + if itype is not None: + self.itype = itype - def __init_insert_data__(self, data): - if data is None: - return + # user created a empty dios: data=None(->inferred=None), itype=None + else: + self._itype = None + def __init_insert_data__(self, data): if isinstance(data, DictOfSeries): for k in data: self[k] = data[k] @@ -112,6 +116,9 @@ class DictOfSeries: for k in self.columns: itypes.append(get_itype(self._data[k].index)) + if not itypes: + return None + found = None # check supertypes @@ -127,7 +134,7 @@ class DictOfSeries: single_itypes = [DatetimeItype, IntegerItype, FloatItype] for single_itype in single_itypes: if all_itypes_le(itypes, single_itype): - found = single_itypes + found = single_itype break return found @@ -291,16 +298,17 @@ class DictOfSeries: if not isinstance(v, pd.Series): raise ValueError(f"Only pd.Series and DictOfSeries (of length 1) can be assigned new") + itype = get_itype(v.index) + if self._itype is None: - # if the user created a empty dios or - # the last emelent was deleted - self._itype = get_itype(v.index) + # if the user created a empty dios + self._itype = itype v = cast_to_fit_itype(v, self._itype) if v is None: - itype = get_itype(v.index) raise ValueError(f"Itype mismach. Data of key `{key}`, with (infered) itype `{itype}` " f"cannot be inserted in a dios with itype `{self.itype}`.") + self._data[key] = v.copy(deep=True) def _setitem(self, key, val, sl=None): diff --git a/dios/itypes.py b/dios/itypes.py index 58647bb..9be4ea1 100644 --- a/dios/itypes.py +++ b/dios/itypes.py @@ -59,24 +59,31 @@ class MixedItype(__Itype): def is_itype(obj, itype): """ Check if obj is a instance of the given itype or its str-alias was given""" + + # todo: iter through itype as it could be a tuple, if called like ``is_itype(o, (t1,t2))`` + # user gave a Itype, like ``DatetimeItype`` - if issubclass(obj, itype): + if type(obj) == type and issubclass(obj, itype): return True - # todo: iter through itype as it could be a tuple, if called like ``is_itype(o, (t1,t2))`` + # user gave a string, like 'datetime' if isinstance(obj, str) and obj == itype.name: return True + return False def is_itype_subtype(obj, itype): """ Check if obj is a subclass or a instance of a subclass of the given itype""" + # user gave a subtype, like ``pd.DatetimeIndex`` - if issubclass(obj, itype.subtypes): + if type(obj) == type and issubclass(obj, itype.subtypes): return True + # user gave a instance of a subtype, like ``pd.Series(..).index`` if isinstance(obj, itype.subtypes): return True + return False @@ -94,8 +101,7 @@ def get_itype(obj): - pd.DatetimeIndex and return the according Itype """ - # shortcut - if issubclass(obj, __Itype): + if type(obj) == type and issubclass(obj, __Itype): return obj # check if it is the actual type, not a subtype diff --git a/tests/run_dios.py b/tests/run_dios.py index 6e59b5d..6f3847c 100644 --- a/tests/run_dios.py +++ b/tests/run_dios.py @@ -1,17 +1,17 @@ from dios import * +import numpy as np if __name__ == '__main__': - dios = DictOfSeries(a=[234.54, 5, 5, 4, np.nan, 5, 4, 5]) - # dios['b'] = pd.Series([2,4,4123,122,4], pd.date_range(freq='1d', periods=5, start='2000-01-01')) + dios = DictOfSeries(data=[234.54, 5, 5, 4, np.nan, 5, 4, 5]) + 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() - dios_options[Options.allow_mixed_itypes] = True - a = dios.loc[:] df = pd.DataFrame([1,24,5,456,45], index=pd.date_range(periods=5, freq='1d', start='2000-01-01')) a = df.iloc[:,0] - print(a) + print(dios) exit(4) dios.columns = ['foo', 'bar'] -- GitLab