Skip to content

Commit b6c15ea

Browse files
authored
BUG: Let check_exact_index default to True for integers (#58189)
* Default check_exact_index to True for integers * Fix pyright issue * fix logic for multiindex * Pre-commit stuff * Address review comments --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]>
1 parent e9b0a3c commit b6c15ea

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Other API changes
159159
- Updated :meth:`DataFrame.to_excel` so that the output spreadsheet has no styling. Custom styling can still be done using :meth:`Styler.to_excel` (:issue:`54154`)
160160
- pickle and HDF (``.h5``) files created with Python 2 are no longer explicitly supported (:issue:`57387`)
161161
- pickled objects from pandas version less than ``1.0.0`` are no longer supported (:issue:`57155`)
162+
- when comparing the indexes in :func:`testing.assert_series_equal`, check_exact defaults to True if an :class:`Index` is of integer dtypes. (:issue:`57386`)
162163

163164
.. ---------------------------------------------------------------------------
164165
.. _whatsnew_300.deprecations:

pandas/_testing/asserters.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -861,12 +861,19 @@ def assert_series_equal(
861861
check_names : bool, default True
862862
Whether to check the Series and Index names attribute.
863863
check_exact : bool, default False
864-
Whether to compare number exactly.
864+
Whether to compare number exactly. This also applies when checking
865+
Index equivalence.
865866
866867
.. versionchanged:: 2.2.0
867868
868869
Defaults to True for integer dtypes if none of
869870
``check_exact``, ``rtol`` and ``atol`` are specified.
871+
872+
.. versionchanged:: 3.0.0
873+
874+
check_exact for comparing the Indexes defaults to True by
875+
checking if an Index is of integer dtypes.
876+
870877
check_datetimelike_compat : bool, default False
871878
Compare datetime-like which is comparable ignoring dtype.
872879
check_categorical : bool, default True
@@ -902,7 +909,6 @@ def assert_series_equal(
902909
>>> tm.assert_series_equal(a, b)
903910
"""
904911
__tracebackhide__ = True
905-
check_exact_index = False if check_exact is lib.no_default else check_exact
906912
if (
907913
check_exact is lib.no_default
908914
and rtol is lib.no_default
@@ -914,8 +920,20 @@ def assert_series_equal(
914920
or is_numeric_dtype(right.dtype)
915921
and not is_float_dtype(right.dtype)
916922
)
923+
left_index_dtypes = (
924+
[left.index.dtype] if left.index.nlevels == 1 else left.index.dtypes
925+
)
926+
right_index_dtypes = (
927+
[right.index.dtype] if right.index.nlevels == 1 else right.index.dtypes
928+
)
929+
check_exact_index = all(
930+
dtype.kind in "iu" for dtype in left_index_dtypes
931+
) or all(dtype.kind in "iu" for dtype in right_index_dtypes)
917932
elif check_exact is lib.no_default:
918933
check_exact = False
934+
check_exact_index = False
935+
else:
936+
check_exact_index = check_exact
919937

920938
rtol = rtol if rtol is not lib.no_default else 1.0e-5
921939
atol = atol if atol is not lib.no_default else 1.0e-8

pandas/tests/util/test_assert_series_equal.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,44 @@ def test_assert_series_equal_int_tol():
475475
)
476476

477477

478-
def test_assert_series_equal_index_exact_default():
478+
@pytest.mark.parametrize(
479+
"left_idx, right_idx",
480+
[
481+
(
482+
pd.Index([0, 0.2, 0.4, 0.6, 0.8, 1]),
483+
pd.Index(np.linspace(0, 1, 6)),
484+
),
485+
(
486+
pd.MultiIndex.from_arrays([[0, 0, 0, 0, 1, 1], [0, 0.2, 0.4, 0.6, 0.8, 1]]),
487+
pd.MultiIndex.from_arrays([[0, 0, 0, 0, 1, 1], np.linspace(0, 1, 6)]),
488+
),
489+
(
490+
pd.MultiIndex.from_arrays(
491+
[["a", "a", "a", "b", "b", "b"], [1, 2, 3, 4, 5, 10000000000001]]
492+
),
493+
pd.MultiIndex.from_arrays(
494+
[["a", "a", "a", "b", "b", "b"], [1, 2, 3, 4, 5, 10000000000002]]
495+
),
496+
),
497+
pytest.param(
498+
pd.Index([1, 2, 3, 4, 5, 10000000000001]),
499+
pd.Index([1, 2, 3, 4, 5, 10000000000002]),
500+
marks=pytest.mark.xfail(reason="check_exact_index defaults to True"),
501+
),
502+
pytest.param(
503+
pd.MultiIndex.from_arrays(
504+
[[0, 0, 0, 0, 1, 1], [1, 2, 3, 4, 5, 10000000000001]]
505+
),
506+
pd.MultiIndex.from_arrays(
507+
[[0, 0, 0, 0, 1, 1], [1, 2, 3, 4, 5, 10000000000002]]
508+
),
509+
marks=pytest.mark.xfail(reason="check_exact_index defaults to True"),
510+
),
511+
],
512+
)
513+
def test_assert_series_equal_check_exact_index_default(left_idx, right_idx):
479514
# GH#57067
480-
ser1 = Series(np.zeros(6, dtype=int), [0, 0.2, 0.4, 0.6, 0.8, 1])
481-
ser2 = Series(np.zeros(6, dtype=int), np.linspace(0, 1, 6))
515+
ser1 = Series(np.zeros(6, dtype=int), left_idx)
516+
ser2 = Series(np.zeros(6, dtype=int), right_idx)
482517
tm.assert_series_equal(ser1, ser2)
483518
tm.assert_frame_equal(ser1.to_frame(), ser2.to_frame())

0 commit comments

Comments
 (0)