예제 #1
0
 def rmod(self, left, right) -> Union["Series", "Index"]:
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right))))
         return right % left
     else:
         raise TypeError(
             "Modulo can not be applied to %s and the given type." % self.pretty_name)
예제 #2
0
 def rpow(self, left: T_IndexOps, right: Any) -> IndexOpsLike:
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right))))
         return right ** left
     else:
         raise TypeError(
             "Exponentiation can not be applied to %s and the given type." % self.pretty_name
         )
예제 #3
0
 def rfloordiv(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = left.spark.transform(
             lambda scol: scol.cast(as_spark_type(type(right))))
         return right // left
     else:
         raise TypeError(
             "Floor division can not be applied to %s and the given type." %
             self.pretty_name)
예제 #4
0
 def rmod(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = transform_boolean_operand_to_numeric(
             left, spark_type=as_spark_type(type(right)))
         return right % left
     else:
         raise TypeError(
             "Modulo can not be applied to %s and the given type." %
             self.pretty_name)
예제 #5
0
 def rpow(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     _sanitize_list_like(right)
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = transform_boolean_operand_to_numeric(left, spark_type=as_spark_type(type(right)))
         return right ** left
     else:
         raise TypeError(
             "Exponentiation can not be applied to %s and the given type." % self.pretty_name
         )
예제 #6
0
 def rmul(self, left, right) -> Union["Series", "Index"]:
     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 right * left
     else:
         raise TypeError(
             "Multiplication can not be applied to %s and the given type." % self.pretty_name
         )
예제 #7
0
 def radd(self, left: T_IndexOps, right: Any) -> IndexOpsLike:
     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 right + left
     else:
         raise TypeError(
             "Addition can not be applied to %s and the given type." % self.pretty_name
         )
예제 #8
0
 def floordiv(self, left, right) -> Union["Series", "Index"]:
     if not is_valid_operand_for_numeric_arithmetic(right, allow_bool=False):
         raise TypeError(
             "Floor division can not be applied to %s and the given type." % self.pretty_name)
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right))))
         return left // right
     else:
         assert isinstance(right, IndexOpsMixin)
         left = transform_boolean_operand_to_numeric(left, right.spark.data_type)
         return left // right
예제 #9
0
 def rmul(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     if isinstance(right, bool):
         return left.__and__(right)
     elif isinstance(right, numbers.Number):
         left = transform_boolean_operand_to_numeric(
             left, spark_type=as_spark_type(type(right)))
         return right * left
     else:
         raise TypeError(
             "Multiplication can not be applied to %s and the given type." %
             self.pretty_name)
예제 #10
0
 def sub(self, left: T_IndexOps, right: Any) -> IndexOpsLike:
     if not is_valid_operand_for_numeric_arithmetic(right, allow_bool=False):
         raise TypeError(
             "Subtraction can not be applied to %s and the given type." % self.pretty_name
         )
     if isinstance(right, numbers.Number) and not isinstance(right, bool):
         left = left.spark.transform(lambda scol: scol.cast(as_spark_type(type(right))))
         return left - right
     else:
         assert isinstance(right, IndexOpsMixin)
         left = transform_boolean_operand_to_numeric(left, right.spark.data_type)
         return left - right
예제 #11
0
 def sub(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     _sanitize_list_like(right)
     if not is_valid_operand_for_numeric_arithmetic(right, allow_bool=False):
         raise TypeError(
             "Subtraction can not be applied to %s and the given type." % self.pretty_name
         )
     if isinstance(right, numbers.Number):
         left = transform_boolean_operand_to_numeric(left, spark_type=as_spark_type(type(right)))
         return left - right
     else:
         assert isinstance(right, IndexOpsMixin)
         left = transform_boolean_operand_to_numeric(left, spark_type=right.spark.data_type)
         return left - right
예제 #12
0
 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
예제 #13
0
 def mul(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
     _sanitize_list_like(right)
     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 = transform_boolean_operand_to_numeric(left, spark_type=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, spark_type=right.spark.data_type)
             return left * right
예제 #14
0
def is_name_like_tuple(value: Any,
                       allow_none: bool = True,
                       check_type: bool = False) -> bool:
    """
    Check the given tuple is be able to be used as a name.

    Examples
    --------
    >>> is_name_like_tuple(('abc',))
    True
    >>> is_name_like_tuple((1,))
    True
    >>> is_name_like_tuple(('abc', 1, None))
    True
    >>> is_name_like_tuple(('abc', 1, None), check_type=True)
    True
    >>> is_name_like_tuple((1.0j,))
    True
    >>> is_name_like_tuple(tuple())
    False
    >>> is_name_like_tuple((list('abc'),))
    False
    >>> is_name_like_tuple(('abc', 1, None), allow_none=False)
    False
    >>> is_name_like_tuple((1.0j,), check_type=True)
    False
    """
    if value is None:
        return allow_none
    elif not isinstance(value, tuple):
        return False
    elif len(value) == 0:
        return False
    elif not allow_none and any(v is None for v in value):
        return False
    elif any(is_list_like(v) or isinstance(v, slice) for v in value):
        return False
    elif check_type:
        return all(
            v is None or as_spark_type(type(v), raise_error=False) is not None
            for v in value)
    else:
        return True
예제 #15
0
    def add(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex:
        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
예제 #16
0
파일: utils.py 프로젝트: jerqi/spark
def is_name_like_value(
    value: Any, allow_none: bool = True, allow_tuple: bool = True, check_type: bool = False
) -> bool:
    """
    Check the given value is like a name.

    Examples
    --------
    >>> is_name_like_value('abc')
    True
    >>> is_name_like_value(1)
    True
    >>> is_name_like_value(None)
    True
    >>> is_name_like_value(('abc',))
    True
    >>> is_name_like_value(1.0j)
    True
    >>> is_name_like_value(list('abc'))
    False
    >>> is_name_like_value(None, allow_none=False)
    False
    >>> is_name_like_value(('abc',), allow_tuple=False)
    False
    >>> is_name_like_value(1.0j, check_type=True)
    False
    """
    if value is None:
        return allow_none
    elif isinstance(value, tuple):
        return allow_tuple and is_name_like_tuple(
            value, allow_none=allow_none, check_type=check_type
        )
    elif is_list_like(value) or isinstance(value, slice):
        return False
    elif check_type:
        return as_spark_type(type(value), raise_error=False) is not None
    else:
        return True