Skip to content
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

DEPR: Timestamp comparison with pydate #49394

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Removal of prior version deprecations/changes
- Changed behavior of :class:`Index` constructor when passed a ``SparseArray`` or ``SparseDtype`` to retain that dtype instead of casting to ``numpy.ndarray`` (:issue:`43930`)
- 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`)
- 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`)
- 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`)
-

Expand Down
14 changes: 6 additions & 8 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]])
Expand Down
42 changes: 14 additions & 28 deletions pandas/tests/scalar/timestamp/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down