[docs]def__call__(self,a:OperationT,b:OperationT)->OperationT:"""Performs an operation on `a` and `b`. Args: a (OperationT): A. b (OperationT): B. Returns: OperationT: Returns the result of the operation. """_op=getattr(operator,f'__{self.value}__')return_op(a,b)
[docs]classLogicalOperation(Operation):"""Defines a logical operation between operands. Attributes: OR: logical or AND: logical and XOR: XOR operation .. versionadded:: 0.6.0 """OR:str='or'AND:str='and'XOR:str='xor'
[docs]classRelationalOperation(Operation):"""Defines a comparison between two operands. Attributes: EQ: Equality NE: Not equal GT: Greater than GE: Greater equal LT: Less than LE: Less equal .. versionadded:: 0.6.0 """EQ='eq'NE='ne'GT='gt'GE='ge'LT='lt'LE='le'
[docs]classNewFeatureModel(BaseModel):"""Defines new features for the :class:`FeatureCreator`. Attributes: name (str): Name of the new feature. features (list[str]): Name of the features to combine. op (LogicalOperation | NumericalOperation | RelationalOperation): Operation to apply to features. keep (bool): Whether to keep feature in the final output. Default: True. .. versionadded:: 0.6.0 """name:str=Field(description='Name of the new feature.',)ifPYDANTIC_V2:features:T.List[str]=Field(min_length=2,description='List of features names.',)else:features:T.List[str]=Field(# type: ignore[no-redef]...,# type: ignore[call-overload]min_items=2,# type: ignore[call-arg]description='List of features names.',)op:T.Union[LogicalOperation,NumericalOperation,RelationalOperation]=(Field(description='Operation to apply to the features.',))keep:bool=Field(True,description='Whether to keep the feature. If False, the result is ''only available as an intermediary feature.',)
[docs]defcalculate(self,data:pd.DataFrame)->pd.Series:"""Calculates the new feature from the given features. Args: data (pd.DataFrame): Given features. Raises: ValueError: Raised if less than 2 features are given. Returns: pd.Series: Returns the new feature. """missing=sorted(list(set(self.features)-set(data.columns)))iflen(missing)>0:raiseValueError(f'Missing columns {missing} in input. 'f'Available columns: {sorted(list(data.columns))}.')result=data.loc[:,self.features[0]]for_,seriesindata.loc[:,self.features[1:]].items():result=self.op(result,series)returnresult