Skip to content

Instantly share code, notes, and snippets.

@sebastianschramm
Created August 30, 2022 16:34
Show Gist options
  • Save sebastianschramm/4c2205f54a0cc997b79f087ec2ddd76c to your computer and use it in GitHub Desktop.
Save sebastianschramm/4c2205f54a0cc997b79f087ec2ddd76c to your computer and use it in GitHub Desktop.
How to use pandas testing assert_frame_equal
import pandas as pd
from pandas.testing import assert_frame_equal
df_one = pd.DataFrame(
{"city": ["Tokyo", "Delhi"], "population": [13_515_271, 16_753_235]}
)
df_two = df_one[["population", "city"]].copy(deep=True)
assert_frame_equal(left=df_one, right=df_two)
"""
AssertionError: DataFrame.columns are different
DataFrame.columns values are different (100.0 %)
[left]: Index(['city', 'population'], dtype='object')
[right]: Index(['population', 'city'], dtype='object')
"""
# enforce order of columns to be the same in df_two
assert_frame_equal(left=df_one, right=df_two[["city", "population"]])
# ignoring order of columns with check_like=True, default is False
assert_frame_equal(left=df_one, right=df_two, check_like=True)
@sebastianschramm
Copy link
Author

dependencies: python3.10, pandas==1.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment