[docs]classColumn(_Operation):""" :py:class:`~onetick.py.Source` column container. This is the object you get when using :py:meth:`~onetick.py.Source.__getitem__`. You can use this object everywhere where :py:class:`~onetick.py.Operation` object can be used. Examples -------- >>> t = otp.Tick(A=1) >>> t['A'] Column(A, <class 'int'>) """def__init__(self,name,dtype=float,obj_ref=None,precision=None):ifnotdtypeornotinspect.isclass(dtype)ornotott.is_type_supported(dtype):raiseTypeError(f'Column does not support "{dtype}" type')self.name=namesuper().__init__(dtype=dtype,obj_ref=obj_ref,op_str=name)# optional propertiesifprecisionisnotNone:ifissubclass(dtype,float):self._precision=precisionelse:raiseValueError("precision is supported only for columns with float or decimal dtypes")defrename(self,new_name,update_parent_object=True):ifself.obj_refandupdate_parent_object:self.obj_ref.rename({self.name:new_name},inplace=True)self.name=new_namedef__len__(self):ifissubclass(self.dtype,str):ifissubclass(self.dtype,ott.string):returnself.dtype.lengthelse:returnott.string.DEFAULT_LENGTHelse:raiseTypeError(f'It is not applicable for the column with type {self.dtype}')# TODO: testdef__hash__(self):returnhash(self.name)def__str__(self):ifnotself.obj_ref:returnself.nameresult=''ifself.obj_ref.use_name_for_column_prefix():ifself.obj_ref.node_name().strip()=='':raiseException('You set to use name for column prefix, but name is empty')result=self.obj_ref.node_name()+'.'result+=self.namereturnresultdef__repr__(self):returnf"Column({str(self)}, {self.dtype})"defcopy(self,obj_ref=None):return_Column(self.name,self.dtype,obj_ref)def__bool__(self):if_Column.emulation_enabled:ifissubclass(self.dtype,int):return(self!=0).__bool__()ifissubclass(self.dtype,float):return(self!=0).__bool__()ifissubclass(self.dtype,str):return(self!="").__bool__()raiseTypeError("It is not allowed to use columns in if-else and while clauses")
[docs]def__getitem__(self,item):""" Provides an ability to get values from future or past ticks. - Negative values refer to past ticks - Zero to current tick - Positive - future ticks Boundary values will be defaulted. For instance for ``item=-1`` first tick value will be defaulted (there is no tick before first tick) Parameters ---------- item: int number of ticks to look back/forward Returns ------- Operation Examples -------- >>> data = otp.Ticks({'A': [1, 2, 3]}) >>> data['PAST1'] = data['A'][-1] >>> data['PAST2'] = data['A'][-2] >>> data['FUTURE1'] = data['A'][1] >>> data['FUTURE2'] = data['A'][2] >>> otp.run(data) Time A PAST1 PAST2 FUTURE1 FUTURE2 0 2003-12-01 00:00:00.000 1 0 0 2 3 1 2003-12-01 00:00:00.001 2 1 0 3 0 2 2003-12-01 00:00:00.002 3 2 1 0 0 """ifnotisinstance(item,int):raiseTypeError("Lag operation supports only integer const values,"f" but passed value of type '{type(item)}'")ifitem==0:returnselfreturn_LagOperator(self,item)
[docs]defcumsum(self):""" Cumulative sum of the column. Can only be used when creating or updating column. Examples -------- >>> t = otp.Ticks({'A': [1, 2, 3]}) >>> t['X'] = t['A'].cumsum() >>> otp.run(t) Time A X 0 2003-12-01 00:00:00.000 1 1.0 1 2003-12-01 00:00:00.001 2 3.0 2 2003-12-01 00:00:00.002 3 6.0 """importonetick.pyasotpreturn_ColumnAggregation(otp.agg.sum(self.name,running=True,all_fields=True,overwrite_output_field=True))
def__iter__(self):raiseTypeError("It is not allowed to use columns in for-clauses")
class_LagOperator(_Operation):""" Implements referencing to the prior tick """def__init__(self,base_column,inx):self._inx=inxop_str=f"{str(base_column)}[{self.index}]"super().__init__(op_params=[base_column],dtype=base_column.dtype,op_str=op_str,obj_ref=base_column.obj_ref)@propertydefindex(self):returnself._inxclass_ColumnAggregation:""" Object to specify how column will be aggregated. """def__init__(self,aggregation):from..aggregations._baseimport_Aggregationifnotisinstance(aggregation,_Aggregation):raiseValueError(f'Expected aggregation object, got {type(aggregation)}')ifnotaggregation.runningornotaggregation.all_fieldsornotaggregation.overwrite_output_field:raiseValueError("Column aggregations only support 'running' aggregations"" with 'all_fields' and 'overwrite_output_field' parameters set")self.aggregation=aggregationdefapply(self,src,name):returnself.aggregation.apply(src,name=name,inplace=True)_Column=Column# alias for backward compatibility