Skip to content

Commit

Permalink
Backport PR pandas-dev#54880: REGR: comparing datetime vs None raises
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and meeseeksmachine committed Sep 1, 2023
1 parent cbff8f1 commit f43a0f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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 @@ -943,8 +944,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)

0 comments on commit f43a0f6

Please sign in to comment.