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

added copy_empty(), warn/err/ignore option

parent 7fb7eb6a
No related branches found
No related tags found
2 merge requests!2Develop,!1complete rework
......@@ -175,8 +175,9 @@ class DictOfSeries:
self._itype = itype
if not itype.unique:
throw(f"Using a {itype} as dios.itype is experimental. As soon as series with different index types "
f"are inserted, slicing will almost always fail. You are hereby warned!", ItypeWarning)
throwMixedItypeErrWarn(f"Using a {itype} as dios.itype is experimental. As soon as series with "
f"different index types are inserted, slicing will almost always fail. "
f"You are hereby warned!")
def __cast_all(self, itype):
for k in self.columns:
......@@ -223,14 +224,14 @@ class DictOfSeries:
return self._data[key]
def _getitem_listlike(self, keys):
new = DictOfSeries()
new = self.copy_empty()
for k in keys:
new[k] = self._get_item(k)
return new
def _slice(self, keys, slicer):
""" Return a slice of self"""
new = DictOfSeries()
new = self.copy_empty()
for k in keys:
new[k] = self._get_item(k)[slicer]
return new
......@@ -411,9 +412,13 @@ class DictOfSeries:
def __deepcopy__(self, memo=None):
return self.copy(deep=True)
def copy(self, deep=True):
def copy_empty(self):
new = DictOfSeries()
new._itype = self.itype
return new
def copy(self, deep=True):
new = self.copy_empty()
# We use `_data` here, because all checks are already done.
# So this should be much faster, especially, because we use the underlying dict for
# getting and setting the values, instead of ``__setitem__`` and ``__getitem__``.
......@@ -423,13 +428,13 @@ class DictOfSeries:
return new
def __op1__(self, op):
new = DictOfSeries()
new = self.copy_empty()
for k in self.columns:
new[k] = op(self[k])
return new
def __op2__(self, other, op):
new = DictOfSeries()
new = self.copy_empty()
if isinstance(other, DictOfSeries):
keys = get_dios_to_dios_keys(self.columns, other)
for k in keys:
......@@ -521,7 +526,7 @@ class DictOfSeries:
:return:
"""
news = pd.Series()
newd = DictOfSeries()
newd = self.copy_empty()
need_dios = False
# return a copy nevertheless, but also run inplace if inplace=True or
# if the function not has this option, but work inplace.
......@@ -571,7 +576,7 @@ class _LocIndexer(_Indexer):
def __getitem__(self, key):
rkey, cols = self._unpack_key(key)
new = DictOfSeries()
new = self._dios.copy_empty()
for c in cols:
new[c] = self._data[c].loc[rkey]
return new
......@@ -634,7 +639,7 @@ class _iLocIndexer(_Indexer):
def __getitem__(self, key):
rkey, cols = self._unpack_key(key)
new = DictOfSeries()
new = self._dios.copy_empty()
for c in cols:
new[c] = self._data[c].iloc[rkey]
return new
......
......@@ -8,8 +8,14 @@ def _get_storage_class_values(cls):
return [getattr(cls, c) for c in cls.__dict__ if not c.startswith("_")]
def throw(msg, wtype):
warnings.warn(msg, wtype)
def throwMixedItypeErrWarn(msg):
if dios_options[Options.mixed_itype_policy] == 'ignore':
pass
elif dios_options[Options.mixed_itype_policy] == 'error':
raise ItypeCastError(msg)
else:
warnings.warn(msg, ItypeWarning)
return
# todo: make method an kwarg and remove dios_options access
......
......@@ -23,10 +23,12 @@ class Options:
otherwise its the same than creating a new dios)"""
dios_to_dios_method = "dios_to_dios_method"
mixed_itype_policy = "mixed_itype_policy"
# set default values
dios_options = {
Options.disp_max_rows: 10,
Options.disp_max_vars: 4,
Options.dios_to_dios_method: 3,
Options.mixed_itype_policy: 'warn',
}
......@@ -3,6 +3,8 @@ from dios import *
import numpy as np
if __name__ == '__main__':
dios_options[Options.mixed_itype_policy] = 'ignore'
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
......
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