def __getitem__(self, item): if isinstance(item, str): return CalculatedStream([item]) elif list_of_instance(item, str): return CalculatedStream(item) elif list_of_instance(item, datetime) or isinstance( item, DatetimeIndex): return self.all(dates=item) elif isinstance(item, slice): # do your handling for a slice object: print(item.start, item.stop, item.step)
def __contains__(self, item): if isinstance(item, datetime): return item in self.dates elif list_of_instance(item, datetime): return all([item in self.dates]) else: return item in self.events
def __getitem__(self, item): from stateful.storage.calculated_stream import CalculatedStream if (isinstance(item, str) and ("/" in item or "-" in item)) or isinstance(item, datetime): date = pd.to_datetime(item, utc=True) return self.get(date, include_date=False, include_id=False) if list_of_instance(item, datetime): return list(self.all(item)) elif isinstance(item, str): assert item in self.controller.keys, f"{item} not valid a stream" return CalculatedStream(dependencies=[item], parent=self) elif list_of_instance(item, str): for name in item: assert name in self.controller.keys, f"{name} not valid a stream" return CalculatedStream(dependencies=item, parent=self)
def df(self) -> pd.DataFrame: if self.empty: return pd.DataFrame() index, values = zip(*list(self._tree)) if list_of_instance(values, dict): return pd.DataFrame(values, index=index) else: return pd.DataFrame(columns={self.name: values}, index=index)
def __getitem__(self, item): if isinstance(self._state, dict): if isinstance(item, str): return self._state.get(item, np.NaN) if list_of_instance(item, str): return Event(date=self.date, state={name: self[name] for name in item}) else: raise AttributeError(f"{self.value} is not a dictionary")
def __getitem__(self, item): if isinstance(item, datetime): i = self.dates[self.dates == item][0] return {name: self._state[name][i] for name in self._state.keys()} if list_of_instance(item, str): return EventFrame(self.dates, {name: self._state[name] for name in item}) return self._state[item]
def head(self, n=5) -> pd.DataFrame: if self.empty: return pd.DataFrame() index, values = [], [] iterator = iter(self._tree) while len(index) < n: idx, value = next(iterator) index.append(idx) values.append(value) if list_of_instance(values, dict): return pd.DataFrame(values, index=index) else: name = self.name if self.name else "values" columns = [{name: v} for v in values] return pd.DataFrame(columns, index=index)
def _stream_conf(self, key): if list_of_instance(key, str): key = "_".join(key) stream_conf = self.configuration.get(key, {}) stream_conf['on_dublicate'] = 'increment' return stream_conf