Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas unit test AssertionError: DataFrame.index are different

I have this function that I want to test:

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to reset the index in 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

Alternatively, if this is intended behavior of the function, edit the expected DF to have the correct index

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading