From 1f9bfe294631298f4a40993cc24b8d9777b3d264 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 29 Oct 2022 20:34:54 -0700 Subject: [PATCH] DEPR: Timestamp comparison with pydate --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/_libs/tslibs/timestamps.pyx | 14 +++---- pandas/tests/indexing/test_loc.py | 3 -- .../scalar/timestamp/test_comparisons.py | 42 +++++++------------ 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0004bc92b349e..6928853480327 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -271,6 +271,7 @@ Removal of prior version deprecations/changes - Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`) - Changed behavior of :class:`Index` constructor when passed a ``SparseArray`` or ``SparseDtype`` to retain that dtype instead of casting to ``numpy.ndarray`` (:issue:`43930`) - Changed behavior of :meth:`DataFrame.any` and :meth:`DataFrame.all` with ``bool_only=True``; object-dtype columns with all-bool values will no longer be included, manually cast to ``bool`` dtype first (:issue:`46188`) +- Changed behavior of comparison of a :class:`Timestamp` with a ``datetime.date`` object; these now compare as un-equal and raise on inequality comparisons, matching the ``datetime.datetime`` behavior (:issue:`36131`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 3c3bb8496aa6e..1a2e2760d3d8d 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -364,15 +364,13 @@ cdef class _Timestamp(ABCTimestamp): # which incorrectly drops tz and normalizes to midnight # before comparing # We follow the stdlib datetime behavior of never being equal - warnings.warn( - "Comparison of Timestamp with datetime.date is deprecated in " - "order to match the standard library behavior. " - "In a future version these will be considered non-comparable. " - "Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.", - FutureWarning, - stacklevel=find_stack_level(), + if op == Py_EQ: + return False + elif op == Py_NE: + return True + raise TypeError("Cannot compare Timestamp with datetime.date. " + "Use ts == pd.Timestamp(date) or ts.date() == date instead." ) - return NotImplemented else: return NotImplemented diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 88ae431fb1baf..ad3cec5824619 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1462,9 +1462,6 @@ def test_loc_setitem_datetime_coercion(self): assert Timestamp("2008-08-08") == df.loc[0, "c"] assert Timestamp("2008-08-08") == df.loc[1, "c"] df.loc[2, "c"] = date(2005, 5, 5) - with tm.assert_produces_warning(FutureWarning): - # Comparing Timestamp to date obj is deprecated - assert Timestamp("2005-05-05") == df.loc[2, "c"] assert Timestamp("2005-05-05").date() == df.loc[2, "c"] @pytest.mark.parametrize("idxer", ["var", ["var"]]) diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index 2c9b029bf109e..ad629604d1bc9 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -156,36 +156,22 @@ def test_compare_date(self, tz): # GH#36131 comparing Timestamp with date object is deprecated ts = Timestamp("2021-01-01 00:00:00.00000", tz=tz) dt = ts.to_pydatetime().date() - # These are incorrectly considered as equal because they - # dispatch to the date comparisons which truncates ts + # in 2.0 we disallow comparing pydate objects with Timestamps, + # following the stdlib datetime behavior. + msg = "Cannot compare Timestamp with datetime.date" for left, right in [(ts, dt), (dt, ts)]: - with tm.assert_produces_warning(FutureWarning): - assert left == right - with tm.assert_produces_warning(FutureWarning): - assert not left != right - with tm.assert_produces_warning(FutureWarning): - assert not left < right - with tm.assert_produces_warning(FutureWarning): - assert left <= right - with tm.assert_produces_warning(FutureWarning): - assert not left > right - with tm.assert_produces_warning(FutureWarning): - assert left >= right - - # Once the deprecation is enforced, the following assertions - # can be enabled: - # assert not left == right - # assert left != right - # - # with pytest.raises(TypeError): - # left < right - # with pytest.raises(TypeError): - # left <= right - # with pytest.raises(TypeError): - # left > right - # with pytest.raises(TypeError): - # left >= right + assert not left == right + assert left != right + + with pytest.raises(TypeError, match=msg): + left < right + with pytest.raises(TypeError, match=msg): + left <= right + with pytest.raises(TypeError, match=msg): + left > right + with pytest.raises(TypeError, match=msg): + left >= right def test_cant_compare_tz_naive_w_aware(self, utc_fixture): # see GH#1404