diff --git a/pandas/_testing.py b/pandas/_testing.py index 7786eeeb46797..f96645b3805f0 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1294,6 +1294,8 @@ def assert_series_equal( rtol=1.0e-5, atol=1.0e-8, obj="Series", + *, + check_index=True, ): """ Check that left and right Series are equal. @@ -1353,6 +1355,10 @@ def assert_series_equal( obj : str, default 'Series' Specify object name being compared, internally used to show appropriate assertion message. + check_index : bool, default True + Whether to check index equivalence. If False, then compare only values. + + .. versionadded:: 1.3.0 Examples -------- @@ -1388,18 +1394,20 @@ def assert_series_equal( if check_flags: assert left.flags == right.flags, f"{repr(left.flags)} != {repr(right.flags)}" - # index comparison - assert_index_equal( - left.index, - right.index, - exact=check_index_type, - check_names=check_names, - check_exact=check_exact, - check_categorical=check_categorical, - rtol=rtol, - atol=atol, - obj=f"{obj}.index", - ) + if check_index: + # GH #38183 + assert_index_equal( + left.index, + right.index, + exact=check_index_type, + check_names=check_names, + check_exact=check_exact, + check_categorical=check_categorical, + rtol=rtol, + atol=atol, + obj=f"{obj}.index", + ) + if check_freq and isinstance(left.index, (pd.DatetimeIndex, pd.TimedeltaIndex)): lidx = left.index ridx = right.index @@ -1704,6 +1712,10 @@ def assert_frame_equal( assert col in right lcol = left.iloc[:, i] rcol = right.iloc[:, i] + # GH #38183 + # use check_index=False, because we do not want to run + # assert_index_equal for each column, + # as we already checked it for the whole dataframe before. assert_series_equal( lcol, rcol, @@ -1717,6 +1729,7 @@ def assert_frame_equal( obj=f'{obj}.iloc[:, {i}] (column name="{col}")', rtol=rtol, atol=atol, + check_index=False, )