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

implemented rudimentary __setattr__

parent 4acf0ea4
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import datetime as dt
from collections import OrderedDict
from itertools import islice
import operator as op
import functools
dios_options = dict(
disp_max_rows=10,
......@@ -37,7 +38,7 @@ class DictOfSeries(OrderedDict):
if isinstance(k, str):
if k in self.__keys:
keygen = [k]
elif isinstance(value, (pd.Series, DictOfSeries)):
else:
self.__addnew(k, value)
return
else:
......@@ -56,12 +57,17 @@ class DictOfSeries(OrderedDict):
if isinstance(v, DictOfSeries):
v = value.squeeze()
if v is value:
# squeeze failed to squeeze
raise ValueError(f"only DictOfSeries of length 1 can be assigned new")
if isinstance(v, list):
v = pd.Series(v)
if isinstance(v, pd.Series):
super().__setitem__(key, v.copy(deep=True))
self.__keys.append(key)
return
else:
raise ValueError(f"Only pd.Series and DictOfSeries (of length 1) can be assigned new")
......@@ -162,6 +168,9 @@ class DictOfSeries(OrderedDict):
pass
def __str__(self):
return self.__repr__()
def __repr__(self):
max_rosw = pd.get_option('display.max_rows')
pd.set_option('display.max_rows', dios_options['disp_max_rows'])
......@@ -172,13 +181,13 @@ class DictOfSeries(OrderedDict):
if len(keylist) > max_vars:
i = max_vars // 2
for k in keylist[:i]:
s += f'{k}:\n{self[k]}\n'
kstr = str(self[k]).replace('\n', '\n ')
s += f'{k}:\n {kstr}\n'
s += ' ... \t\t\t\t ...\n' * 2
keylist = keylist[-i:]
for k in keylist:
s += f'{k}:\n{self[k]}\n'
except Exception:
raise
kstr = str(self[k]).replace('\n', ' \n ')
s += f'{k}:\n {kstr}\n'
finally:
pd.set_option('display.max_rows', max_rosw)
return s
......@@ -268,6 +277,18 @@ class DictOfSeries(OrderedDict):
def __xor__(self, other):
return self.__op2__(other, op.xor)
def __getattr__(self, item):
# this is called only(!) if the requested attribute was not found
# (implicit handled by self.__getattribute__).
attr = getattr(pd.Series, item, None)
if attr is None:
raise AttributeError(f"'DictOfSeries' nor 'pd.Series' objects have an attribute '{item}'")
if hasattr(attr, '__call__'):
return functools.partial(self.pipe, attr)
else:
raise NotImplementedError(f"cannot propagate non-functions to items of dios")
def squeeze(self):
if len(self) == 1:
return self[self.__keys[0]]
......@@ -325,4 +346,10 @@ class DictOfSeries(OrderedDict):
if __name__ == '__main__':
pass
dios = DictOfSeries(a=[234.54,5,5,4,np.nan, 5,4,5])
dios['b'] = dios *2
dios.squeeze()
print(dios)
dios.dropna(inplace=True)
print(dios)
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