@@ -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 (
0 commit comments