要选择我使用的数据框行:
dataframe[dataframe['column_name'] == 'column_value']
我试图发现此代码的性能,但无法找到 API 在查找行的上下文中定义 == 方法的位置。
如何找到==运算符的来源以确定上述代码行的性能?
我假设它位于 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html但不确定如何搜索?
请您参考如下方法:
首先,请注意 == 运算符在后台调用 __eq__。在您的上下文中,它与 pd.Series 对象相关,而不是 pd.DataFrame。所以这些是等价的:
res = pd.Series([1, 2, 3]) == 1
res = pd.Series([1, 2, 3]).__eq__(1)
然后您可以研究这个方法:
pd.Series.__eq__
<function pandas.core.ops._comp_method_SERIES.<locals>.wrapper>
此外,您可以在 ops.py 中调查 _comp_method_SERIES:
def _comp_method_SERIES(op, name, str_rep, masker=False):
"""
Wrapper function for Series arithmetic operations, to avoid
code duplication.
"""
....
这应该可以帮助您入门。定义了效用函数,例如处理空值,它表示 Pandas 为数据操作添加的值。优化的操作可能最终会调用 C 级算法,这可能会使纯 Python 中的跟踪性能问题变得困难。
