Skip to content

Commit eb69d89

Browse files
authored
DEPR: Timestamp comparison with pydate (#49394)
1 parent 60013a2 commit eb69d89

File tree

4 files changed

+21
-39
lines changed

4 files changed

+21
-39
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ Removal of prior version deprecations/changes
287287
- Changed behavior of :class:`Index` constructor when passed a ``SparseArray`` or ``SparseDtype`` to retain that dtype instead of casting to ``numpy.ndarray`` (:issue:`43930`)
288288
- Removed the deprecated ``base`` and ``loffset`` arguments from :meth:`pandas.DataFrame.resample`, :meth:`pandas.Series.resample` and :class:`pandas.Grouper`. Use ``offset`` or ``origin`` instead (:issue:`31809`)
289289
- 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`)
290+
- 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`)
290291
- Enforced deprecation of silently dropping columns that raised a ``TypeError`` in :class:`Series.transform` and :class:`DataFrame.transform` when used with a list or dictionary (:issue:`43740`)
291292
-
292293

pandas/_libs/tslibs/timestamps.pyx

+6-8
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,13 @@ cdef class _Timestamp(ABCTimestamp):
364364
# which incorrectly drops tz and normalizes to midnight
365365
# before comparing
366366
# We follow the stdlib datetime behavior of never being equal
367-
warnings.warn(
368-
"Comparison of Timestamp with datetime.date is deprecated in "
369-
"order to match the standard library behavior. "
370-
"In a future version these will be considered non-comparable. "
371-
"Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.",
372-
FutureWarning,
373-
stacklevel=find_stack_level(),
367+
if op == Py_EQ:
368+
return False
369+
elif op == Py_NE:
370+
return True
371+
raise TypeError("Cannot compare Timestamp with datetime.date. "
372+
"Use ts == pd.Timestamp(date) or ts.date() == date instead."
374373
)
375-
return NotImplemented
376374
else:
377375
return NotImplemented
378376

pandas/tests/indexing/test_loc.py

-3
Original file line numberDiff line numberDiff line change
@@ -1462,9 +1462,6 @@ def test_loc_setitem_datetime_coercion(self):
14621462
assert Timestamp("2008-08-08") == df.loc[0, "c"]
14631463
assert Timestamp("2008-08-08") == df.loc[1, "c"]
14641464
df.loc[2, "c"] = date(2005, 5, 5)
1465-
with tm.assert_produces_warning(FutureWarning):
1466-
# Comparing Timestamp to date obj is deprecated
1467-
assert Timestamp("2005-05-05") == df.loc[2, "c"]
14681465
assert Timestamp("2005-05-05").date() == df.loc[2, "c"]
14691466

14701467
@pytest.mark.parametrize("idxer", ["var", ["var"]])

pandas/tests/scalar/timestamp/test_comparisons.py

+14-28
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,22 @@ def test_compare_date(self, tz):
156156
# GH#36131 comparing Timestamp with date object is deprecated
157157
ts = Timestamp("2021-01-01 00:00:00.00000", tz=tz)
158158
dt = ts.to_pydatetime().date()
159-
# These are incorrectly considered as equal because they
160-
# dispatch to the date comparisons which truncates ts
159+
# in 2.0 we disallow comparing pydate objects with Timestamps,
160+
# following the stdlib datetime behavior.
161161

162+
msg = "Cannot compare Timestamp with datetime.date"
162163
for left, right in [(ts, dt), (dt, ts)]:
163-
with tm.assert_produces_warning(FutureWarning):
164-
assert left == right
165-
with tm.assert_produces_warning(FutureWarning):
166-
assert not left != right
167-
with tm.assert_produces_warning(FutureWarning):
168-
assert not left < right
169-
with tm.assert_produces_warning(FutureWarning):
170-
assert left <= right
171-
with tm.assert_produces_warning(FutureWarning):
172-
assert not left > right
173-
with tm.assert_produces_warning(FutureWarning):
174-
assert left >= right
175-
176-
# Once the deprecation is enforced, the following assertions
177-
# can be enabled:
178-
# assert not left == right
179-
# assert left != right
180-
#
181-
# with pytest.raises(TypeError):
182-
# left < right
183-
# with pytest.raises(TypeError):
184-
# left <= right
185-
# with pytest.raises(TypeError):
186-
# left > right
187-
# with pytest.raises(TypeError):
188-
# left >= right
164+
assert not left == right
165+
assert left != right
166+
167+
with pytest.raises(TypeError, match=msg):
168+
left < right
169+
with pytest.raises(TypeError, match=msg):
170+
left <= right
171+
with pytest.raises(TypeError, match=msg):
172+
left > right
173+
with pytest.raises(TypeError, match=msg):
174+
left >= right
189175

190176
def test_cant_compare_tz_naive_w_aware(self, utc_fixture):
191177
# see GH#1404

0 commit comments

Comments
 (0)