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

BUG: Timestamp == date match stdlib #36131

Merged
merged 64 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
4c5eddd
REF: remove unnecesary try/except
jbrockmendel Aug 21, 2020
c632c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
9e64be3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
42649fb
TST: add test for agg on ordered categorical cols (#35630)
mathurk1 Aug 21, 2020
47121dd
TST: resample does not yield empty groups (#10603) (#35799)
tkmz-n Aug 21, 2020
1decb3e
revert accidental rebase
jbrockmendel Aug 22, 2020
57c5dd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 22, 2020
a358463
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
ffa7ad7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
e5e98d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
408db5a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
9b1b3f3
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Aug 26, 2020
a6d4228
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 4, 2020
5517a68
BUG: Timestamp == date comparison match stdlib
jbrockmendel Sep 4, 2020
5ce9683
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 11, 2020
f43e920
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 12, 2020
1bffa44
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 13, 2020
8c6012e
Deprecate
jbrockmendel Sep 13, 2020
ec827a0
isort troubleshoot
jbrockmendel Sep 13, 2020
fbb1576
catch warnings
jbrockmendel Sep 13, 2020
efae3ad
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 13, 2020
e53dc16
suggested edit
jbrockmendel Sep 13, 2020
e504852
troubleshoot isort
jbrockmendel Sep 14, 2020
5b34905
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 20, 2020
f0060e3
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 21, 2020
f97848e
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 22, 2020
41ae9cd
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 26, 2020
f0add43
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 26, 2020
da0888c
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Sep 29, 2020
27c8005
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 1, 2020
8036c99
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 3, 2020
a8fed20
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 11, 2020
198d9e0
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 14, 2020
67429a1
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 31, 2020
3513f3b
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 31, 2020
6df2513
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Oct 31, 2020
dbb3086
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Nov 10, 2020
87ff44a
split+fixturize
jbrockmendel Nov 10, 2020
c97a1e7
asserts for warnings
jbrockmendel Nov 10, 2020
67b3fb2
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Nov 11, 2020
da9426c
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Nov 25, 2020
13f170b
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Nov 25, 2020
ba18c96
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Nov 28, 2020
de69fb4
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 6, 2020
d412ac0
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 8, 2020
cf85a4f
move whatnsew
jbrockmendel Dec 8, 2020
a1e32d7
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 17, 2020
938ad02
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 23, 2020
06589ca
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 23, 2020
4cd919c
tc1 -> cat_series1
jbrockmendel Dec 23, 2020
947daac
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 27, 2020
5196b1e
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 27, 2020
4d7aa41
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 29, 2020
a307a0c
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 29, 2020
a92409d
revert drop_duplicates tests
jbrockmendel Dec 30, 2020
164e2b4
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 30, 2020
a321fdd
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 30, 2020
0cb6019
Update pandas/_libs/tslibs/timestamps.pyx
jbrockmendel Dec 30, 2020
3557c7a
Merge branch 'bug-timestamp-cmp-date' of github.com:jbrockmendel/pand…
jbrockmendel Dec 30, 2020
af49cb5
catch warning
jbrockmendel Dec 30, 2020
7a423f4
lint fixup
jbrockmendel Dec 30, 2020
677760f
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 31, 2020
b8c67e9
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 31, 2020
fbfdaff
suppress warning
jbrockmendel Dec 31, 2020
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/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Deprecations
- Deprecated allowing subclass-specific keyword arguments in the :class:`Index` constructor, use the specific subclass directly instead (:issue:`14093`,:issue:`21311`,:issue:`22315`,:issue:`26974`)
- Deprecated ``astype`` of datetimelike (``timedelta64[ns]``, ``datetime64[ns]``, ``Datetime64TZDtype``, ``PeriodDtype``) to integer dtypes, use ``values.view(...)`` instead (:issue:`38544`)
- Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`)
- Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`)
-

.. ---------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ from numpy cimport int8_t, int64_t, ndarray, uint8_t
cnp.import_array()

from cpython.datetime cimport ( # alias bc `tzinfo` is a kwarg below
PyDate_Check,
PyDateTime_Check,
PyDateTime_IMPORT,
PyDelta_Check,
Expand Down Expand Up @@ -281,6 +282,20 @@ cdef class _Timestamp(ABCTimestamp):
return np.zeros(other.shape, dtype=np.bool_)
return NotImplemented

elif PyDate_Check(other):
# returning NotImplemented defers to the `date` implementation
# 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=1,
)
return NotImplemented
else:
return NotImplemented

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,10 @@ 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)
assert Timestamp("2005-05-05") == df.loc[2, "c"]
with tm.assert_produces_warning(FutureWarning):
# Comparing Timestamp to date obj is deprecated
assert Timestamp("2005-05-05") == df.loc[2, "c"]
jreback marked this conversation as resolved.
Show resolved Hide resolved
assert Timestamp("2005-05-05").date() == df.loc[2, "c"]

def test_loc_setitem_datetimelike_with_inference(self):
# GH 7592
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,13 @@ def test_get_indexer_mixed_dtypes(self, target):
([date(9999, 1, 1), date(9999, 1, 1)], [-1, -1]),
],
)
# FIXME: these warnings are flaky GH#36131
@pytest.mark.filterwarnings(
"ignore:Comparison of Timestamp with datetime.date:FutureWarning"
)
def test_get_indexer_out_of_bounds_date(self, target, positions):
values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")])

result = values.get_indexer(target)
expected = np.array(positions, dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/scalar/timestamp/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,42 @@ def test_compare_invalid(self):
assert val != np.float64(1)
assert val != np.int64(1)

@pytest.mark.parametrize("tz", [None, "US/Pacific"])
def test_compare_date(self, tz):
# GH#36131 comparing Timestamp with date object is deprecated
ts = Timestamp.now(tz)
dt = ts.to_pydatetime().date()
# These are incorrectly considered as equal because they
# dispatch to the date comparisons which truncates ts

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

def test_cant_compare_tz_naive_w_aware(self, utc_fixture):
# see GH#1404
a = Timestamp("3/12/2012")
Expand Down