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

fixed init's

parent df3dbe9e
No related branches found
No related tags found
1 merge request!218Flags
...@@ -92,6 +92,7 @@ class Flags: ...@@ -92,6 +92,7 @@ class Flags:
result = {} result = {}
for obj in data: for obj in data:
if isinstance(obj, tuple): if isinstance(obj, tuple):
k, item = obj k, item = obj
else: else:
...@@ -102,6 +103,10 @@ class Flags: ...@@ -102,6 +103,10 @@ class Flags:
if isinstance(item, pd.Series): if isinstance(item, pd.Series):
item = item.to_frame(name=0) item = item.to_frame(name=0)
elif isinstance(item, History):
pass
else:
raise TypeError(f"cannot init from {type(data.__name__)} of {type(item.__name__)}")
result[k] = History(item, copy=copy) result[k] = History(item, copy=copy)
...@@ -140,17 +145,21 @@ class Flags: ...@@ -140,17 +145,21 @@ class Flags:
def columns(self, value: pd.Index): def columns(self, value: pd.Index):
if not isinstance(value, pd.Index): if not isinstance(value, pd.Index):
value = pd.Index(value) value = pd.Index(value)
if ( if (
not value.is_unique not value.is_unique
or not pd.api.types.is_string_dtype(value) or not pd.api.types.is_string_dtype(value)
): ):
raise TypeError('value must be pd.Index, with unique indices of type str') raise TypeError('value must be pd.Index, with unique indices of type str')
if not len(value) == len(self): if not len(value) == len(self):
raise ValueError("index must match current index in length") raise ValueError("index must match current index in length")
_data, _cache = {}, {} _data, _cache = {}, {}
for old, new in zip(self.columns, value): for old, new in zip(self.columns, value):
_data[new] = self._data.pop(old) _data[new] = self._data.pop(old)
if old in self._cache: if old in self._cache:
_cache[new] = self._cache[old] _cache[new] = self._cache[old]
...@@ -234,7 +243,7 @@ def init_flags_like(reference: Union[pd.Series, DictLike, Flags], initial_value: ...@@ -234,7 +243,7 @@ def init_flags_like(reference: Union[pd.Series, DictLike, Flags], initial_value:
if not isinstance(item, (pd.Series, History)): if not isinstance(item, (pd.Series, History)):
raise TypeError('items in reference must be of type pd.Series') raise TypeError('items in reference must be of type pd.Series')
item = pd.DataFrame(UNFLAGGED, index=item.index, columns=[0], dtype=float) item = pd.DataFrame(initial_value, index=item.index, columns=[0], dtype=float)
result[k] = History(item) result[k] = History(item)
...@@ -244,6 +253,7 @@ def init_flags_like(reference: Union[pd.Series, DictLike, Flags], initial_value: ...@@ -244,6 +253,7 @@ def init_flags_like(reference: Union[pd.Series, DictLike, Flags], initial_value:
if __name__ == '__main__': if __name__ == '__main__':
from dios import example_DictOfSeries from dios import example_DictOfSeries
f = init_flags_like(example_DictOfSeries()) f = init_flags_like(example_DictOfSeries(), initial_value=-3.)
print(f) print(f)
print(Flags()) print(Flags())
print(Flags(example_DictOfSeries().astype(float)))
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