modin/pandas/series.py
def apply(
        self, func, convert_dtype=True, args=(), **kwargs
    ):  # noqa: PR01, RT01, D200
        """
        Invoke function on values of Series.
        """
        self._validate_function(func)
        # apply and aggregate have slightly different behaviors, so we have to use
        # each one separately to determine the correct return type. In the case of
        # `agg`, the axis is set, but it is not required for the computation, so we use
        # it to determine which function to run.
        if kwargs.pop("axis", None) is not None:
            apply_func = "agg"
        else:
            apply_func = "apply"

        # This is the simplest way to determine the return type, but there are checks
        # in pandas that verify that some results are created. This is a challenge for
        # empty DataFrames, but fortunately they only happen when the `func` type is
        # a list or a dictionary, which means that the return type won't change from
        # type(self), so we catch that error and use `type(self).__name__` for the return
        # type.
        # We create a "dummy" `Series` to do the error checking and determining
        # the return type.
        try:
            return_type = type(
                getattr(
                    pandas.Series(self[:1].values, index=self.index[:1]), apply_func
                )(func, *args, **kwargs)
            ).__name__
        except Exception:
            return_type = type(self).__name__
        if (
            isinstance(func, str)
            or is_list_like(func)
            or return_type not in ["DataFrame", "Series"]
        ):
            result = super(Series, self).apply(func, *args, **kwargs)
        else:
            # handle ufuncs and lambdas
            if kwargs or args and not isinstance(func, np.ufunc):

                def f(x):
                    return func(x, *args, **kwargs)

            else:
                f = func
            with np.errstate(all="ignore"):
                if isinstance(f, np.ufunc):
                    return f(self)
                result = self.map(f)._query_compiler
        if return_type not in ["DataFrame", "Series"]:
            # sometimes result can be not a query_compiler, but scalar (for example
            # for sum or count functions)
            if isinstance(result, type(self._query_compiler)):
                return result.to_pandas().squeeze()
            else:
                return result
        else:
            result = getattr(sys.modules[self.__module__], return_type)(
                query_compiler=result
            )
            if result.name == self.index[0]:
                result.name = None
            return result

        Invoke function on values of Series.
        
Invoke function on values of Series.
Invoke function on values of Series .
def apply(
        self, func, convert_dtype=True, args=(), **kwargs
    ):  
        self._validate_function(func)
        if kwargs.pop("axis", None) is not None:
            apply_func = "agg"
        else:
            apply_func = "apply"
        try:
            return_type = type(
                getattr(
                    pandas.Series(self[:1].values, index=self.index[:1]), apply_func
                )(func, *args, **kwargs)
            ).__name__
        except Exception:
            return_type = type(self).__name__
        if (
            isinstance(func, str)
            or is_list_like(func)
            or return_type not in ["DataFrame", "Series"]
        ):
            result = super(Series, self).apply(func, *args, **kwargs)
        else:
            if kwargs or args and not isinstance(func, np.ufunc):
                def f(x):
                    return func(x, *args, **kwargs)
            else:
                f = func
            with np.errstate(all="ignore"):
                if isinstance(f, np.ufunc):
                    return f(self)
                result = self.map(f)._query_compiler
        if return_type not in ["DataFrame", "Series"]:
            if isinstance(result, type(self._query_compiler)):
                return result.to_pandas().squeeze()
            else:
                return result
        else:
            result = getattr(sys.modules[self.__module__], return_type)(
                query_compiler=result
            )
            if result.name == self.index[0]:
                result.name = None
            return result
def apply ( self , func , convert_dtype = True , args = ( ) , ** kwargs ) : self . _validate_function ( func ) if kwargs . pop ( "axis" , None ) is not None : apply_func = "agg" else : apply_func = "apply" try : return_type = type ( getattr ( pandas . Series ( self [ : 1 ] . values , index = self . index [ : 1 ] ) , apply_func ) ( func , * args , ** kwargs ) ) . __name__ except Exception : return_type = type ( self ) . __name__ if ( isinstance ( func , str ) or is_list_like ( func ) or return_type not in [ "DataFrame" , "Series" ] ) : result = super ( Series , self ) . apply ( func , * args , ** kwargs ) else : if kwargs or args and not isinstance ( func , np . ufunc ) : def f ( x ) : return func ( x , * args , ** kwargs ) else : f = func with np . errstate ( all = "ignore" ) : if isinstance ( f , np . ufunc ) : return f ( self ) result = self . map ( f ) . _query_compiler if return_type not in [ "DataFrame" , "Series" ] : if isinstance ( result , type ( self . _query_compiler ) ) : return result . to_pandas ( ) . squeeze ( ) else : return result else : result = getattr ( sys . modules [ self . __module__ ] , return_type ) ( query_compiler = result ) if result . name == self . index [ 0 ] : result . name = None return result
Invoke function on values of Series.
Invoke function on values of Series .
# noqa: PR01, RT01, D200 """
        Invoke function on values of Series.
        """ # apply and aggregate have slightly different behaviors, so we have to use # each one separately to determine the correct return type. In the case of # `agg`, the axis is set, but it is not required for the computation, so we use # it to determine which function to run. # This is the simplest way to determine the return type, but there are checks # in pandas that verify that some results are created. This is a challenge for # empty DataFrames, but fortunately they only happen when the `func` type is # a list or a dictionary, which means that the return type won't change from # type(self), so we catch that error and use `type(self).__name__` for the return # type. # We create a "dummy" `Series` to do the error checking and determining # the return type. # handle ufuncs and lambdas # sometimes result can be not a query_compiler, but scalar (for example # for sum or count functions)
self
func
convert_dtype
args