Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
REGR: comparing datetime vs None raises
  • Loading branch information
phofl committed Aug 30, 2023
commit 59ac5588eb1eaedb41f3507abf9cebe79f953fdf
9 changes: 7 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
algorithms,
missing,
nanops,
ops,
)
from pandas.core.algorithms import (
checked_add_with_arr,
Expand Down Expand Up @@ -947,8 +948,12 @@ def _cmp_method(self, other, op):

dtype = getattr(other, "dtype", None)
if is_object_dtype(dtype):
return op(np.asarray(self, dtype=object), other)

# We have to use comp_method_OBJECT_ARRAY instead of numpy
# comparison otherwise it would raise when comparing to None
result = ops.comp_method_OBJECT_ARRAY(
op, np.asarray(self.astype(object)), other
)
return result
if other is NaT:
if op is operator.ne:
result = np.ones(self.shape, dtype=bool)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,12 @@ def test_getitem_str_second_with_datetimeindex():
msg = r"Timestamp\('2012-01-02 18:01:02-0600', tz='US/Central'\)"
with pytest.raises(KeyError, match=msg):
df[df.index[2]]


def test_compare_datetime_with_all_none():
# GH#54870
ser = Series(["2020-01-01", "2020-01-02"], dtype="datetime64[ns]")
ser2 = Series([None, None])
result = ser > ser2
expected = Series([False, False])
tm.assert_series_equal(result, expected)