Skip to content

BUG: NaT.__cmp__(invalid) should raise TypeError #35585

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 5 commits into from
Aug 6, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :attr:`DatetimeArray.date` where a ``ValueError`` would be raised with a read-only backing array (:issue:`33530`)
- Bug in ``NaT`` comparisons failing to raise ``TypeError`` on invalid inequality comparisons (:issue:`35046`)
-

Timedelta
Expand Down
31 changes: 13 additions & 18 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,25 @@ cdef class _NaT(datetime):
__array_priority__ = 100

def __richcmp__(_NaT self, object other, int op):
cdef:
int ndim = getattr(other, "ndim", -1)
if util.is_datetime64_object(other) or PyDateTime_Check(other):
# We treat NaT as datetime-like for this comparison
return _nat_scalar_rules[op]

if ndim == -1:
elif util.is_timedelta64_object(other) or PyDelta_Check(other):
# We treat NaT as timedelta-like for this comparison
return _nat_scalar_rules[op]

elif util.is_array(other):
result = np.empty(other.shape, dtype=np.bool_)
result.fill(_nat_scalar_rules[op])
if other.dtype.kind in "mM":
result = np.empty(other.shape, dtype=np.bool_)
result.fill(_nat_scalar_rules[op])
elif other.dtype.kind == "O":
result = np.array([PyObject_RichCompare(self, x, op) for x in other])
else:
return NotImplemented
return result

elif ndim == 0:
if util.is_datetime64_object(other):
return _nat_scalar_rules[op]
else:
raise TypeError(
f"Cannot compare type {type(self).__name__} "
f"with type {type(other).__name__}"
)

# Note: instead of passing "other, self, _reverse_ops[op]", we observe
# that `_nat_scalar_rules` is invariant under `_reverse_ops`,
# rendering it unnecessary.
return PyObject_RichCompare(other, self, op)
return NotImplemented

def __add__(self, other):
if self is not c_NaT:
Expand Down
62 changes: 59 additions & 3 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,67 @@ def test_to_numpy_alias():
assert isna(expected) and isna(result)


@pytest.mark.parametrize("other", [Timedelta(0), Timestamp(0)])
@pytest.mark.parametrize(
"other",
[
Timedelta(0),
Timedelta(0).to_pytimedelta(),
pytest.param(
Timedelta(0).to_timedelta64(),
marks=pytest.mark.xfail(
reason="td64 doesnt return NotImplemented, see numpy#17017"
),
),
Timestamp(0),
Timestamp(0).to_pydatetime(),
pytest.param(
Timestamp(0).to_datetime64(),
marks=pytest.mark.xfail(
reason="dt64 doesnt return NotImplemented, see numpy#17017"
),
),
Timestamp(0).tz_localize("UTC"),
NaT,
],
)
def test_nat_comparisons(compare_operators_no_eq_ne, other):
# GH 26039
assert getattr(NaT, compare_operators_no_eq_ne)(other) is False
assert getattr(other, compare_operators_no_eq_ne)(NaT) is False
opname = compare_operators_no_eq_ne

assert getattr(NaT, opname)(other) is False

op = getattr(operator, opname.strip("_"))
assert op(NaT, other) is False
assert op(other, NaT) is False


@pytest.mark.parametrize("other", [np.timedelta64(0, "ns"), np.datetime64("now", "ns")])
def test_nat_comparisons_numpy(other):
# Once numpy#17017 is fixed and the xfailed cases in test_nat_comparisons
# pass, this test can be removed
assert not NaT == other
assert NaT != other
assert not NaT < other
assert not NaT > other
assert not NaT <= other
assert not NaT >= other


@pytest.mark.parametrize("other", ["foo", 2, 2.0])
@pytest.mark.parametrize("op", [operator.le, operator.lt, operator.ge, operator.gt])
def test_nat_comparisons_invalid(other, op):
# GH#35585
assert not NaT == other
assert not other == NaT

assert NaT != other
assert other != NaT

with pytest.raises(TypeError):
op(NaT, other)

with pytest.raises(TypeError):
op(other, NaT)


@pytest.mark.parametrize(
Expand Down