From fc14198410209a282baf0f521eb1328355fdf543 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 23 Jan 2020 04:54:59 +0200 Subject: [PATCH 1/3] COMPAT: tzawareness behavior to be same as datetime --- pandas/_libs/tslibs/c_timestamp.pyx | 7 ++++--- .../scalar/timestamp/test_comparisons.py | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pandas/_libs/tslibs/c_timestamp.pyx b/pandas/_libs/tslibs/c_timestamp.pyx index ed1df5f4fa595..26b45e3231f71 100644 --- a/pandas/_libs/tslibs/c_timestamp.pyx +++ b/pandas/_libs/tslibs/c_timestamp.pyx @@ -119,7 +119,9 @@ cdef class _Timestamp(datetime): else: return NotImplemented - self._assert_tzawareness_compat(other) + if op not in [Py_EQ, Py_NE]: + self._assert_tzawareness_compat(other) + return cmp_scalar(self.value, ots.value, op) def __reduce_ex__(self, protocol): @@ -179,8 +181,7 @@ cdef class _Timestamp(datetime): cdef _assert_tzawareness_compat(_Timestamp self, datetime other): if self.tzinfo is None: if other.tzinfo is not None: - raise TypeError('Cannot compare tz-naive and tz-aware ' - 'timestamps') + raise TypeError("Cannot compare tz-naive and tz-aware timestamps") elif other.tzinfo is None: raise TypeError('Cannot compare tz-naive and tz-aware timestamps') diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index fce4fa6eb1eaa..d11111df4d5ed 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -85,10 +85,6 @@ def test_cant_compare_tz_naive_w_aware(self, utc_fixture): a = Timestamp("3/12/2012") b = Timestamp("3/12/2012", tz=utc_fixture) - with pytest.raises(TypeError): - a == b - with pytest.raises(TypeError): - a != b with pytest.raises(TypeError): a < b with pytest.raises(TypeError): @@ -98,10 +94,6 @@ def test_cant_compare_tz_naive_w_aware(self, utc_fixture): with pytest.raises(TypeError): a >= b - with pytest.raises(TypeError): - b == a - with pytest.raises(TypeError): - b != a with pytest.raises(TypeError): b < a with pytest.raises(TypeError): @@ -161,6 +153,17 @@ def test_compare_zerodim_array(self): result = arr > ts assert result is False + def test_timestamp_compare_tz(self): + # https://github.com/pandas-dev/pandas/issues/28507 + ts = Timestamp.now() + ts2 = ts.tz_localize("UTC") + + dt = ts.to_pydatetime() + dt2 = ts2.to_pydatetime() + + assert ts == ts2 + assert not dt == dt2 + def test_rich_comparison_with_unsupported_type(): # Comparisons with unsupported objects should return NotImplemented From ccc991d08380f3022c17b172d130600e3eda4932 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 24 Jan 2020 14:28:01 +0200 Subject: [PATCH 2/3] Fixed some failing tests --- pandas/tests/arithmetic/test_datetime64.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index d3f9ac4f3f8b2..317745f318372 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -526,7 +526,7 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): @pytest.mark.parametrize( "op", - [operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le], + [operator.gt, operator.ge, operator.lt, operator.le], ) def test_comparison_tzawareness_compat(self, op, box_df_fail): # GH#18162 From 977cb557aac074ad60bdacef4946b46bd549478d Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 24 Jan 2020 16:12:28 +0200 Subject: [PATCH 3/3] Applying what jbrockmendel said REF: https://github.com/pandas-dev/pandas/pull/31239/files#r369940591 --- pandas/_libs/tslibs/c_timestamp.pyx | 6 ++++-- pandas/tests/scalar/timestamp/test_comparisons.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/c_timestamp.pyx b/pandas/_libs/tslibs/c_timestamp.pyx index 63f6bd3695ca3..96dd54e38204a 100644 --- a/pandas/_libs/tslibs/c_timestamp.pyx +++ b/pandas/_libs/tslibs/c_timestamp.pyx @@ -119,9 +119,11 @@ cdef class _Timestamp(datetime): else: return NotImplemented - if op not in [Py_EQ, Py_NE]: - self._assert_tzawareness_compat(other) + if op in [Py_EQ, Py_NE]: + return op == Py_NE + + self._assert_tzawareness_compat(other) return cmp_scalar(self.value, ots.value, op) def __reduce_ex__(self, protocol): diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index d11111df4d5ed..8a58c17ded1a2 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -161,7 +161,10 @@ def test_timestamp_compare_tz(self): dt = ts.to_pydatetime() dt2 = ts2.to_pydatetime() - assert ts == ts2 + assert ts != ts2 + assert dt != dt2 + + assert not ts == ts2 assert not dt == dt2