Why are numpy functions so slow on pandas series / dataframes?

后端 未结 4 1227
耶瑟儿~
耶瑟儿~ 2020-12-28 12:10

Consider a small MWE, taken from another question:

DateTime                Data
2017-11-21 18:54:31     1
2017-11-22 02:26:48     2
2017-11-22 10:19:44     3         


        
4条回答
  •  [愿得一人]
    2020-12-28 13:14

    Just read the source code, it's clear.

    def clip(a, a_min, a_max, out=None):
        """a : array_like Array containing elements to clip."""
        return _wrapfunc(a, 'clip', a_min, a_max, out=out)
    
    def _wrapfunc(obj, method, *args, **kwds):
        try:
            return getattr(obj, method)(*args, **kwds)
        #This situation has occurred in the case of
        # a downstream library like 'pandas'.
        except (AttributeError, TypeError):
            return _wrapit(obj, method, *args, **kwds)
    
    def _wrapit(obj, method, *args, **kwds):
        try:
            wrap = obj.__array_wrap__
        except AttributeError:
            wrap = None
        result = getattr(asarray(obj), method)(*args, **kwds)
        if wrap:
            if not isinstance(result, mu.ndarray):
                result = asarray(result)
            result = wrap(result)
        return result
    

    rectify:

    after pandas v0.13.0_ahl1,pandas has it's own implement of clip.

提交回复
热议问题