diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a619b08a8676f..bfaeefbdcfa72 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1149,6 +1149,7 @@ Other - Bug in ``divmod`` and ``rdivmod`` with :class:`DataFrame`, :class:`Series`, and :class:`Index` with ``bool`` dtypes failing to raise, which was inconsistent with ``__floordiv__`` behavior (:issue:`46043`) - Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`) - Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`) +- Deprecated the keyword ``check_datetimelike_compat`` in :meth:`testing.assert_frame_equal` and :meth:`testing.assert_series_equal` (:issue:`55638`) - Fixed bug where the :class:`DataFrame` constructor misclassified array-like objects with a ``.name`` attribute as :class:`Series` or :class:`Index` (:issue:`61443`) - Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index daa5187cdb636..1e03d60041875 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -7,6 +7,7 @@ NoReturn, cast, ) +import warnings import numpy as np @@ -15,6 +16,8 @@ from pandas._libs.sparse import SparseIndex import pandas._libs.testing as _testing from pandas._libs.tslibs.np_datetime import compare_mismatched_resolutions +from pandas.errors import Pandas4Warning +from pandas.util._decorators import deprecate_kwarg from pandas.core.dtypes.common import ( is_bool, @@ -843,6 +846,7 @@ def assert_extension_array_equal( # This could be refactored to use the NDFrame.equals method +@deprecate_kwarg(Pandas4Warning, "check_datetimelike_compat", new_arg_name=None) def assert_series_equal( left, right, @@ -897,6 +901,9 @@ def assert_series_equal( check_datetimelike_compat : bool, default False Compare datetime-like which is comparable ignoring dtype. + + .. deprecated:: 3.0 + check_categorical : bool, default True Whether to compare internal Categorical exactly. check_category_order : bool, default True @@ -1132,6 +1139,7 @@ def assert_series_equal( # This could be refactored to use the NDFrame.equals method +@deprecate_kwarg(Pandas4Warning, "check_datetimelike_compat", new_arg_name=None) def assert_frame_equal( left, right, @@ -1194,6 +1202,9 @@ def assert_frame_equal( ``check_exact``, ``rtol`` and ``atol`` are specified. check_datetimelike_compat : bool, default False Compare datetime-like which is comparable ignoring dtype. + + .. deprecated:: 3.0 + check_categorical : bool, default True Whether to compare internal Categorical exactly. check_like : bool, default False @@ -1320,22 +1331,28 @@ def assert_frame_equal( # 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, - check_dtype=check_dtype, - check_index_type=check_index_type, - check_exact=check_exact, - check_names=check_names, - check_datetimelike_compat=check_datetimelike_compat, - check_categorical=check_categorical, - check_freq=check_freq, - obj=f'{obj}.iloc[:, {i}] (column name="{col}")', - rtol=rtol, - atol=atol, - check_index=False, - check_flags=False, - ) + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + message="the 'check_datetimelike_compat' keyword", + category=Pandas4Warning, + ) + assert_series_equal( + lcol, + rcol, + check_dtype=check_dtype, + check_index_type=check_index_type, + check_exact=check_exact, + check_names=check_names, + check_datetimelike_compat=check_datetimelike_compat, + check_categorical=check_categorical, + check_freq=check_freq, + obj=f'{obj}.iloc[:, {i}] (column name="{col}")', + rtol=rtol, + atol=atol, + check_index=False, + check_flags=False, + ) def assert_equal(left, right, **kwargs) -> None: diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index ea954756d63c8..8711365a19214 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -1,5 +1,7 @@ import pytest +from pandas.errors import Pandas4Warning + import pandas as pd from pandas import DataFrame import pandas._testing as tm @@ -395,3 +397,19 @@ def test_assert_frame_equal_set_mismatch(): msg = r'DataFrame.iloc\[:, 0\] \(column name="set_column"\) values are different' with pytest.raises(AssertionError, match=msg): tm.assert_frame_equal(df1, df2) + + +def test_datetimelike_compat_deprecated(): + # GH#55638 + df = DataFrame({"a": [1]}) + + msg = "the 'check_datetimelike_compat' keyword is deprecated" + with tm.assert_produces_warning(Pandas4Warning, match=msg): + tm.assert_frame_equal(df, df, check_datetimelike_compat=True) + with tm.assert_produces_warning(Pandas4Warning, match=msg): + tm.assert_frame_equal(df, df, check_datetimelike_compat=False) + + with tm.assert_produces_warning(Pandas4Warning, match=msg): + tm.assert_series_equal(df["a"], df["a"], check_datetimelike_compat=True) + with tm.assert_produces_warning(Pandas4Warning, match=msg): + tm.assert_series_equal(df["a"], df["a"], check_datetimelike_compat=False)