我有以下功能
def main():
(
pd.DataFrame({'a': [1, 2, float('NaN')], 'b': [1.0, 2, 3]})
.dropna(subset=['a'])
.assign(
b=lambda x: x['b'] * 2
)
.apply(do_something_with_each_row, axis='columns')
)
def do_something_with_each_row(one_row):
# do_something_with_row
print(one_row)
在我的测试中,我想查看在所有链接操作之后构建的数据框,并在调用之前检查它是否一切正常do_something_with_each_row
。最后一个函数不返回数据帧(它只是迭代所有行,类似于iterrow
)。
我试图模拟这样的apply
函数:
# need pytest-mock and pytest
import pandas as pd
def test_not_working(mocker):
mocked_apply = mocker.patch.object(pd.Dataframe, 'apply')
main()
但在这种情况下,我无法访问输入apply
以测试其内容的数据框。
我还尝试模拟do_something_with_each_row
:
# need pytest-mock and pytest
import pandas as pd
def test_not_working_again(mocker):
mocked_to_something = mocker.patch('path.to.file.do_something_with_each_row')
main()
但是这次我有所有带有行参数的调用,但它们都有None
值。
如何获取apply
调用函数的数据帧并检查它是否确实与以下内容相同:
pd.Dataframe({'a': [1, 2], 'b': [2.0, 4]})
我正在使用0.24.2
pandas 版本,升级到 pandas1.0.5
并不会改变此事。
我尝试在熊猫问题中进行搜索,但没有找到有关此主题的任何内容。