Skip to content

REGR: comparing datetime vs None raises #54880

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 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)

- Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`)
.. ---------------------------------------------------------------------------
.. _whatsnew_211.bug_fixes:

Expand Down
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)