Skip to content

Commit

Permalink
Option to check column order when comparing polars dataframes (#3206)
Browse files Browse the repository at this point in the history
  • Loading branch information
physinet committed Apr 25, 2022
1 parent 558adc0 commit 929ec8c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def assert_frame_equal(
right: DataFrame,
check_dtype: bool = True,
check_exact: bool = False,
check_column_names: bool = True,
rtol: float = 1.0e-5,
atol: float = 1.0e-8,
) -> None:
Expand All @@ -53,6 +54,8 @@ def assert_frame_equal(
if True, data types need to match exactly
check_exact
if False, test if values are within tolerance of each other (see `rtol` & `atol`)
check_column_names
if True, dataframes must have the same column names in the same order
rtol
relative tolerance for inexact checking. Fraction of values in `right`
atol
Expand All @@ -70,7 +73,6 @@ def assert_frame_equal(
"""

obj = "DataFrame"
check_column_order = True

if not (isinstance(left, DataFrame) and isinstance(right, DataFrame)):
raise_assert_detail(obj, "Type mismatch", type(left), type(right))
Expand All @@ -90,9 +92,9 @@ def assert_frame_equal(
f"column {c} in right dataframe, but not in left dataframe"
)

if check_column_order:
if check_column_names:
if left.columns != right.columns:
raise AssertionError("Columns are not in same order")
raise AssertionError("Columns are not in the same order")

# this does not assume a particular order
for col in left.columns:
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ def test_assert_frame_equal_column_mismatch_order() -> None:
df2 = pl.DataFrame({"a": [1, 2], "b": [3, 4]})
with pytest.raises(AssertionError):
pl.testing.assert_frame_equal(df1, df2)
pl.testing.assert_frame_equal(df1, df2, check_column_names=False)

0 comments on commit 929ec8c

Please sign in to comment.