Skip to content

BUG: Timestamp comparison with ndarray[dt64] #33346

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

Merged
merged 1 commit into from
Apr 9, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ cdef class _Timestamp(datetime):
return NotImplemented
elif is_array(other):
# avoid recursion error GH#15183
if other.dtype.kind == "M":
if self.tz is None:
return PyObject_RichCompare(self.asm8, other, op)
raise TypeError(
"Cannot compare tz-naive and tz-aware timestamps"
)
if other.dtype.kind == "O":
# Operate element-wise
return np.array(
[PyObject_RichCompare(self, x, op) for x in other],
dtype=bool,
)
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
Expand Down
52 changes: 52 additions & 0 deletions pandas/tests/scalar/timestamp/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,61 @@
import pytest

from pandas import Timestamp
import pandas._testing as tm


class TestTimestampComparison:
def test_comparison_dt64_ndarray(self):
ts = Timestamp.now()
ts2 = Timestamp("2019-04-05")
arr = np.array([[ts.asm8, ts2.asm8]], dtype="M8[ns]")

result = ts == arr
expected = np.array([[True, False]], dtype=bool)
tm.assert_numpy_array_equal(result, expected)

result = arr == ts
tm.assert_numpy_array_equal(result, expected)

result = ts != arr
tm.assert_numpy_array_equal(result, ~expected)

result = arr != ts
tm.assert_numpy_array_equal(result, ~expected)

result = ts2 < arr
tm.assert_numpy_array_equal(result, expected)

result = arr < ts2
tm.assert_numpy_array_equal(result, np.array([[False, False]], dtype=bool))

result = ts2 <= arr
tm.assert_numpy_array_equal(result, np.array([[True, True]], dtype=bool))

result = arr <= ts2
tm.assert_numpy_array_equal(result, ~expected)

result = ts >= arr
tm.assert_numpy_array_equal(result, np.array([[True, True]], dtype=bool))

result = arr >= ts
tm.assert_numpy_array_equal(result, np.array([[True, False]], dtype=bool))

@pytest.mark.parametrize("reverse", [True, False])
def test_comparison_dt64_ndarray_tzaware(self, reverse, all_compare_operators):
op = getattr(operator, all_compare_operators.strip("__"))

ts = Timestamp.now("UTC")
arr = np.array([ts.asm8, ts.asm8], dtype="M8[ns]")

left, right = ts, arr
if reverse:
left, right = arr, ts

msg = "Cannot compare tz-naive and tz-aware timestamps"
with pytest.raises(TypeError, match=msg):
op(left, right)

def test_comparison_object_array(self):
# GH#15183
ts = Timestamp("2011-01-03 00:00:00-0500", tz="US/Eastern")
Expand Down