Skip to content

COMPAT: tzawareness behavior to be same as datetime #31239

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ cdef class _Timestamp(datetime):
else:
return NotImplemented


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we dont have tzawareness compat here, then eq should be False and ne should be True, i.e. dont just fall through to cmp_scalar here

if op in [Py_EQ, Py_NE]:
return op == Py_NE

self._assert_tzawareness_compat(other)
return cmp_scalar(self.value, ots.value, op)

Expand Down Expand Up @@ -179,8 +183,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')

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions pandas/tests/scalar/timestamp/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -161,6 +153,20 @@ 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 dt != dt2

assert not ts == ts2
assert not dt == dt2


def test_rich_comparison_with_unsupported_type():
# Comparisons with unsupported objects should return NotImplemented
Expand Down