Skip to content

Commit 1dd97e2

Browse files
ivanovmgluckyvs1
authored andcommitted
PERF: fix assert_frame_equal can be very slow (pandas-dev#38202)
1 parent 81481d1 commit 1dd97e2

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

pandas/_testing.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,8 @@ def assert_series_equal(
12941294
rtol=1.0e-5,
12951295
atol=1.0e-8,
12961296
obj="Series",
1297+
*,
1298+
check_index=True,
12971299
):
12981300
"""
12991301
Check that left and right Series are equal.
@@ -1353,6 +1355,10 @@ def assert_series_equal(
13531355
obj : str, default 'Series'
13541356
Specify object name being compared, internally used to show appropriate
13551357
assertion message.
1358+
check_index : bool, default True
1359+
Whether to check index equivalence. If False, then compare only values.
1360+
1361+
.. versionadded:: 1.3.0
13561362
13571363
Examples
13581364
--------
@@ -1388,18 +1394,20 @@ def assert_series_equal(
13881394
if check_flags:
13891395
assert left.flags == right.flags, f"{repr(left.flags)} != {repr(right.flags)}"
13901396

1391-
# index comparison
1392-
assert_index_equal(
1393-
left.index,
1394-
right.index,
1395-
exact=check_index_type,
1396-
check_names=check_names,
1397-
check_exact=check_exact,
1398-
check_categorical=check_categorical,
1399-
rtol=rtol,
1400-
atol=atol,
1401-
obj=f"{obj}.index",
1402-
)
1397+
if check_index:
1398+
# GH #38183
1399+
assert_index_equal(
1400+
left.index,
1401+
right.index,
1402+
exact=check_index_type,
1403+
check_names=check_names,
1404+
check_exact=check_exact,
1405+
check_categorical=check_categorical,
1406+
rtol=rtol,
1407+
atol=atol,
1408+
obj=f"{obj}.index",
1409+
)
1410+
14031411
if check_freq and isinstance(left.index, (pd.DatetimeIndex, pd.TimedeltaIndex)):
14041412
lidx = left.index
14051413
ridx = right.index
@@ -1704,6 +1712,10 @@ def assert_frame_equal(
17041712
assert col in right
17051713
lcol = left.iloc[:, i]
17061714
rcol = right.iloc[:, i]
1715+
# GH #38183
1716+
# use check_index=False, because we do not want to run
1717+
# assert_index_equal for each column,
1718+
# as we already checked it for the whole dataframe before.
17071719
assert_series_equal(
17081720
lcol,
17091721
rcol,
@@ -1717,6 +1729,7 @@ def assert_frame_equal(
17171729
obj=f'{obj}.iloc[:, {i}] (column name="{col}")',
17181730
rtol=rtol,
17191731
atol=atol,
1732+
check_index=False,
17201733
)
17211734

17221735

0 commit comments

Comments
 (0)