[英]pandas unit test AssertionError: DataFrame.index are different
I have this function that I want to test:我有这个 function 我想测试:
def filter_df(df, column_name: str, skill: List):
return df.query(f"{column_name} in {skill}")
This is my test:这是我的测试:
def test_filter_df():
df = pd.DataFrame({"col1": ["sap", "hi", "abc"], "col2": [3, 4, 4]})
expected = pd.DataFrame({"col1": ["hi", "abc"], "col2": [4, 4]})
assert_frame_equal(filter_df(df, "col1", ["hi", "abc"]), expected)
I'm getting a assert_frame_equal(filter_df(df, "col1", ["hi", "abc"]), expected)
error, but I don't see why the dataframes aren't identical.我收到
assert_frame_equal(filter_df(df, "col1", ["hi", "abc"]), expected)
错误,但我不明白为什么数据帧不相同。
You need to reset the index in filter_df
:您需要重置
filter_df
中的索引:
df.query(f"{column_name} in {skill}").reset_index(drop=True)
At the moment the returned DF has the original index of the given rows which in your case is 1,2 and not 0,1 as in the expected
DF目前返回的 DF 具有给定行的原始索引,在您的情况下是 1,2 而不是
expected
DF 中的 0,1
Alternatively, if this is intended behavior of the function, edit the expected
DF to have the correct index或者,如果这是 function 的预期行为,请编辑
expected
的 DF 以具有正确的索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.