Skip to content

BUG: Let check_exact_index default to True for integers #58189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Other API changes
- 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`)
- pickle and HDF (``.h5``) files created with Python 2 are no longer explicitly supported (:issue:`57387`)
- pickled objects from pandas version less than ``1.0.0`` are no longer supported (:issue:`57155`)
- when comparing the indexes in :func:`testing.assert_series_equal`, the default value for check_exact is decided based on the :class:`Index` dtype, instead of the :class:`Series` dtype. (:issue:`57386`)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.deprecations:
Expand Down
25 changes: 23 additions & 2 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,19 @@ def assert_series_equal(
check_names : bool, default True
Whether to check the Series and Index names attribute.
check_exact : bool, default False
Whether to compare number exactly.
Whether to compare number exactly. This also applies when checking
Index equivalence.

.. versionchanged:: 2.2.0

Defaults to True for integer dtypes if none of
``check_exact``, ``rtol`` and ``atol`` are specified.

.. versionchanged:: 3.0.0

When checking Index equivalence, the default value for check_exact
is based off the Index dtype, instead of the Series dtype.

check_datetimelike_compat : bool, default False
Compare datetime-like which is comparable ignoring dtype.
check_categorical : bool, default True
Expand Down Expand Up @@ -902,7 +909,6 @@ def assert_series_equal(
>>> tm.assert_series_equal(a, b)
"""
__tracebackhide__ = True
check_exact_index = False if check_exact is lib.no_default else check_exact
if (
check_exact is lib.no_default
and rtol is lib.no_default
Expand All @@ -914,8 +920,23 @@ def assert_series_equal(
or is_numeric_dtype(right.dtype)
and not is_float_dtype(right.dtype)
)
left_index_dtypes = (
[left.index.dtype] if left.index.nlevels == 1 else left.index.dtypes
)
right_index_dtypes = (
[right.index.dtype] if right.index.nlevels == 1 else right.index.dtypes
)
check_exact_index = (
all(is_numeric_dtype(dtype) for dtype in left_index_dtypes)
and not any(is_float_dtype(dtype) for dtype in left_index_dtypes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could check dtype.kind in "iu" to check integer types in one go

Copy link
Member Author

@Aloqeely Aloqeely Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant! That doesn't seem to work for multiindexes (as in I can't just use this on index.dtype) as the dtype is always object (why?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem to work for multiindexes

Probably because MultiIndex can be composed of levels that are mixed types so object is the safest type

or all(is_numeric_dtype(dtype) for dtype in right_index_dtypes)
and not any(is_float_dtype(dtype) for dtype in right_index_dtypes)
)
elif check_exact is lib.no_default:
check_exact = False
check_exact_index = False
else:
check_exact_index = check_exact

rtol = rtol if rtol is not lib.no_default else 1.0e-5
atol = atol if atol is not lib.no_default else 1.0e-8
Expand Down
41 changes: 38 additions & 3 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,44 @@ def test_assert_series_equal_int_tol():
)


def test_assert_series_equal_index_exact_default():
@pytest.mark.parametrize(
"left_idx, right_idx",
[
(
pd.Index([0, 0.2, 0.4, 0.6, 0.8, 1]),
pd.Index(np.linspace(0, 1, 6)),
),
(
pd.MultiIndex.from_arrays([[0, 0, 0, 0, 1, 1], [0, 0.2, 0.4, 0.6, 0.8, 1]]),
pd.MultiIndex.from_arrays([[0, 0, 0, 0, 1, 1], np.linspace(0, 1, 6)]),
),
(
pd.MultiIndex.from_arrays(
[["a", "a", "a", "b", "b", "b"], [1, 2, 3, 4, 5, 10000000000001]]
),
pd.MultiIndex.from_arrays(
[["a", "a", "a", "b", "b", "b"], [1, 2, 3, 4, 5, 10000000000002]]
),
),
pytest.param(
pd.Index([1, 2, 3, 4, 5, 10000000000001]),
pd.Index([1, 2, 3, 4, 5, 10000000000002]),
marks=pytest.mark.xfail(reason="check_exact_index defaults to True"),
),
pytest.param(
pd.MultiIndex.from_arrays(
[[0, 0, 0, 0, 1, 1], [1, 2, 3, 4, 5, 10000000000001]]
),
pd.MultiIndex.from_arrays(
[[0, 0, 0, 0, 1, 1], [1, 2, 3, 4, 5, 10000000000002]]
),
marks=pytest.mark.xfail(reason="check_exact_index defaults to True"),
),
],
)
def test_assert_series_equal_check_exact_index_default(left_idx, right_idx):
# GH#57067
ser1 = Series(np.zeros(6, dtype=int), [0, 0.2, 0.4, 0.6, 0.8, 1])
ser2 = Series(np.zeros(6, dtype=int), np.linspace(0, 1, 6))
ser1 = Series(np.zeros(6, dtype=int), left_idx)
ser2 = Series(np.zeros(6, dtype=int), right_idx)
tm.assert_series_equal(ser1, ser2)
tm.assert_frame_equal(ser1.to_frame(), ser2.to_frame())