Created
August 30, 2022 16:34
-
-
Save sebastianschramm/4c2205f54a0cc997b79f087ec2ddd76c to your computer and use it in GitHub Desktop.
How to use pandas testing assert_frame_equal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dependencies: python3.10, pandas==1.4.3