def _try_aggregate_string_function(self, arg: str, *args, **kwargs):
"""
if arg is a string, then try to operate on it:
- try to find a function (or attribute) on ourselves
- try to find a numpy function
- raise
"""
assert isinstance(arg, str)
f = getattr(self, arg, None)
if f is not None:
if callable(f):
return f(*args, **kwargs)
# people may try to aggregate on a non-callable attribute
# but don't let them think they can pass args to it
assert len(args) == 0
assert len([kwarg for kwarg in kwargs if kwarg not in ["axis"]]) == 0
return f
f = getattr(np, arg, None)
if f is not None:
if hasattr(self, "__array__"):
# in particular exclude Window
return f(self, *args, **kwargs)
raise AttributeError(
f"'{arg}' is not a valid function for '{type(self).__name__}' object"
)
def _get_cython_func(self, arg: Callable) -> Optional[str]:
"""
if we define an internal function for this argument, return it
"""
return self._cython_table.get(arg)
def aggregate(
obj: AggObjType,
arg: AggFuncType,
*args,
**kwargs,
):
...
...
if callable(arg):
f = obj._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(obj, f)(), None
...
...
@Ch3steR,谢谢你帮我看到光明。不过我想详细说明一下你的答案
{a1}包括这些相关行
然后我们追踪^{}
因此,当您进行类似
df.agg('foo')
的调用时,熊猫首先查找名为foo
的数据帧属性,然后查找名为foo
的NumPy函数(假设foo
不作为数据帧属性存在)这是内部细节,我不认为这会被记录下来
pandas dev以这种方式处理这些字符串,即
'sum'
,'mean'
。它们有一个映射,将函数映射到该函数的内部cythonised实现摘自^{}
所以,
Series.agg(sum)
,Series.agg('sum')
,Series.agg(np.sum)
,Series.agg(np.nansum)
都调用相同的内部cythonized函数摘自^{}
你可以在^{} 中找到它们是如何处理的,它们使用} 看看^{}
getattr
在这里,似乎cythonized func是定义的类属性。我没有找到好的起点,但最好是在^{相关问题 更多 >
编程相关推荐