def pow(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: _sanitize_list_like(right) if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError( "Exponentiation can not be applied to given types.") def pow_func(left: Column, right: Any) -> Column: return (F.when(left == 1, left).when(SF.lit(right) == 0, 1).otherwise(Column.__pow__(left, right))) right = transform_boolean_operand_to_numeric( right, spark_type=left.spark.data_type) return column_op(pow_func)(left, right)
def rfloordiv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: _sanitize_list_like(right) if not isinstance(right, numbers.Number): raise TypeError( "Floor division can not be applied to given types.") def rfloordiv(left: Column, right: Any) -> Column: return F.when(SF.lit(left == 0), SF.lit(np.inf).__div__(right)).otherwise( F.when(SF.lit(left) == np.nan, np.nan).otherwise( F.floor(SF.lit(right).__div__(left)))) right = transform_boolean_operand_to_numeric( right, spark_type=left.spark.data_type) return numpy_column_op(rfloordiv)(left, right)
def rtruediv(self, left, right) -> Union["Series", "Index"]: if isinstance(right, str): raise TypeError( "division can not be applied on string series or literals.") if not isinstance(right, numbers.Number): raise TypeError("division can not be applied to given types.") def rtruediv(left, right): return F.when(left == 0, F.lit(np.inf).__div__(right)).otherwise( F.lit(right).__truediv__(left)) right = transform_boolean_operand_to_numeric(right, left.spark.data_type) return numpy_column_op(rtruediv)(left, right)
def rpow(self, left, right) -> Union["Series", "Index"]: if isinstance(right, str): raise TypeError( "exponentiation can not be applied on string series or literals." ) if not isinstance(right, numbers.Number): raise TypeError( "exponentiation can not be applied to given types.") def rpow_func(left, right): return F.when(F.lit(right == 1), right).otherwise(Column.__rpow__(left, right)) right = transform_boolean_operand_to_numeric(right) return column_op(rpow_func)(left, right)
def truediv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError("True division can not be applied to given types.") def truediv(left: Column, right: Any) -> Column: return F.when( SF.lit(right != 0) | SF.lit(right).isNull(), left.__div__(right) ).otherwise( F.when(SF.lit(left == np.inf) | SF.lit(left == -np.inf), left).otherwise( SF.lit(np.inf).__div__(left) ) ) right = transform_boolean_operand_to_numeric(right, spark_type=left.spark.data_type) return numpy_column_op(truediv)(left, right)
def mod(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if (isinstance(right, IndexOpsMixin) and isinstance( right.spark.data_type, StringType)) or isinstance(right, str): raise TypeError( "modulo can not be applied on string series or literals.") if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError("modulo can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, left.spark.data_type) def mod(left: Column, right: Any) -> Column: return ((left % right) + right) % right return column_op(mod)(left, right)
def rfloordiv(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if isinstance(right, str): raise TypeError( "division can not be applied on string series or literals.") if not isinstance(right, numbers.Number): raise TypeError("division can not be applied to given types.") def rfloordiv(left: Column, right: Any) -> Column: return F.when(F.lit(left == 0), F.lit(np.inf).__div__(right)).otherwise( F.when(F.lit(left) == np.nan, np.nan).otherwise( F.floor(F.lit(right).__div__(left)))) right = transform_boolean_operand_to_numeric(right, left.spark.data_type) return numpy_column_op(rfloordiv)(left, right)
def mul(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if isinstance(right, str): raise TypeError( "multiplication can not be applied to a string literal.") if isinstance(right, IndexOpsMixin) and isinstance( right.spark.data_type, TimestampType): raise TypeError("multiplication can not be applied to date times.") if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError( "multiplication can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, left.spark.data_type) return column_op(Column.__mul__)(left, right)
def mul(self, left, right) -> Union["Series", "Index"]: if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError( "Multiplication can not be applied to %s and the given type." % self.pretty_name ) if isinstance(right, bool): return left.__and__(right) elif isinstance(right, numbers.Number): left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right)))) return left * right else: assert isinstance(right, IndexOpsMixin) if isinstance(right, IndexOpsMixin) and isinstance(right.spark.data_type, BooleanType): return left.__and__(right) else: left = transform_boolean_operand_to_numeric(left, right.spark.data_type) return left * right
def truediv(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if (isinstance(right, IndexOpsMixin) and isinstance( right.spark.data_type, StringType)) or isinstance(right, str): raise TypeError( "division can not be applied on string series or literals.") if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError("division can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, left.spark.data_type) def truediv(left: Column, right: Any) -> Column: return F.when( F.lit(right != 0) | F.lit(right).isNull(), left.__div__(right)).otherwise(F.lit(np.inf).__div__(left)) return numpy_column_op(truediv)(left, right)
def add(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError( "Addition can not be applied to %s and the given type." % self.pretty_name ) if isinstance(right, bool): return left.__or__(right) elif isinstance(right, numbers.Number): left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right)))) return left + right else: assert isinstance(right, IndexOpsMixin) if isinstance(right, IndexOpsMixin) and isinstance(right.spark.data_type, BooleanType): return left.__or__(right) else: left = transform_boolean_operand_to_numeric(left, right.spark.data_type) return left + right
def pow(self, left: T_IndexOps, right: Any) -> IndexOpsLike: if (isinstance(right, IndexOpsMixin) and isinstance( right.spark.data_type, StringType)) or isinstance(right, str): raise TypeError( "exponentiation can not be applied on string series or literals." ) if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError( "exponentiation can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, left.spark.data_type) def pow_func(left: Column, right: Any) -> Column: return F.when(left == 1, left).otherwise(Column.__pow__(left, right)) return column_op(pow_func)(left, right)
def floordiv(self, left, right) -> Union["Series", "Index"]: if (isinstance(right, IndexOpsMixin) and isinstance( right.spark.data_type, StringType)) or isinstance(right, str): raise TypeError( "division can not be applied on string series or literals.") if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError("division can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, left.spark.data_type) def floordiv(left, right): return F.when(F.lit(right is np.nan), np.nan).otherwise( F.when( F.lit(right != 0) | F.lit(right).isNull(), F.floor(left.__div__(right))).otherwise( F.lit(np.inf).__div__(left))) return numpy_column_op(floordiv)(left, right)
def sub(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: if not is_valid_operand_for_numeric_arithmetic(right): raise TypeError("Subtraction can not be applied to given types.") right = transform_boolean_operand_to_numeric(right, spark_type=left.spark.data_type) return column_op(Column.__sub__)(left, right)
def rmul(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: if not isinstance(right, numbers.Number): raise TypeError("Multiplication can not be applied to given types.") right = transform_boolean_operand_to_numeric(right) return column_op(Column.__rmul__)(left, right)
def rsub(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: _sanitize_list_like(right) if not isinstance(right, numbers.Number): raise TypeError("Subtraction can not be applied to given types.") right = transform_boolean_operand_to_numeric(right) return column_op(Column.__rsub__)(left, right)