Skip to content

Commit ebd6634

Browse files
authored
tests: fix flakey dataframe tests (#7738)
1 parent 22a9065 commit ebd6634

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

tests/_plugins/ui/_impl/dataframes/test_handlers.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,18 @@ def assert_frame_equal(a: DataFrameType, b: DataFrameType) -> None:
8282
assert nw_a.to_dict(as_series=False) == nw_b.to_dict(as_series=False)
8383

8484

85-
def assert_frame_equal_with_nans(a: DataFrameType, b: DataFrameType) -> None:
85+
def assert_frame_equal_with_nans(
86+
a: DataFrameType, b: DataFrameType, allow_nan_equals_zero: bool = False
87+
) -> None:
8688
"""
8789
Assert two dataframes are equal, treating NaNs in the same locations as equal.
90+
91+
Args:
92+
a: First dataframe
93+
b: Second dataframe
94+
allow_nan_equals_zero: If True, treat NaN and 0.0 as equivalent values.
95+
This is useful for pivot operations where missing aggregations may
96+
be filled with 0.0 or NaN depending on the backend.
8897
"""
8998
import math
9099

@@ -109,7 +118,21 @@ def assert_frame_equal_with_nans(a: DataFrameType, b: DataFrameType) -> None:
109118
and math.isnan(val_a)
110119
and math.isnan(val_b)
111120
)
112-
if not (val_a == val_b or both_nan):
121+
# For pivot operations, treat NaN and 0.0 as equivalent
122+
nan_or_zero_match = (
123+
allow_nan_equals_zero
124+
and isinstance(val_a, (float, int))
125+
and isinstance(val_b, (float, int))
126+
and (
127+
(math.isnan(val_a) if isinstance(val_a, float) else False)
128+
or val_a == 0.0
129+
)
130+
and (
131+
(math.isnan(val_b) if isinstance(val_b, float) else False)
132+
or val_b == 0.0
133+
)
134+
)
135+
if not (val_a == val_b or both_nan or nan_or_zero_match):
113136
raise AssertionError(
114137
f"DataFrame values differ at column '{col}', row {idx}: {val_a} != {val_b}"
115138
)
@@ -1385,7 +1408,11 @@ def test_handle_pivot(
13851408
df: DataFrameType, expected: DataFrameType, transform: PivotTransform
13861409
) -> None:
13871410
result = apply(df, transform)
1388-
assert_frame_equal_with_nans(result, expected)
1411+
# Allow NaN and 0.0 to be treated as equivalent for pivot operations
1412+
# since different backends may fill missing aggregations differently
1413+
assert_frame_equal_with_nans(
1414+
result, expected, allow_nan_equals_zero=True
1415+
)
13891416

13901417
@staticmethod
13911418
@pytest.mark.parametrize(

tests/_plugins/ui/_impl/dataframes/test_print_code.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,14 @@ def test_print_code_result_matches_actual_transform_polars(
572572
# TODO: unimplemented
573573
if transform.type == TransformType.AGGREGATE:
574574
assume(False)
575+
# Don't group by unhashable types (lists, dicts) as ordering is non-deterministic
576+
if transform.type == TransformType.GROUP_BY:
577+
assume(
578+
not any(
579+
column_id in {"lists", "dicts"}
580+
for column_id in transform.column_ids
581+
)
582+
)
575583
if transform.type == TransformType.PIVOT:
576584
assume(
577585
(
@@ -681,8 +689,9 @@ def test_print_code_result_matches_actual_transform_polars(
681689

682690
# For group_by transforms, the row order might differ even with maintain_order=True
683691
# Sort both dataframes by all columns before comparing
692+
# Note: We exclude grouping by unhashable types (lists, dicts) via assume() above,
693+
# so all columns should be sortable
684694
if transform.type == TransformType.GROUP_BY:
685-
# Filter out columns with unhashable types (dicts, lists)
686695
import polars.datatypes as pl_dtypes
687696

688697
sortable_cols = [

0 commit comments

Comments
 (0)