Skip to content

Commit 592126c

Browse files
authored
COMPAT: match numpy behavior for searchsorted on dt64/td64 (#36176)
1 parent 405b5c5 commit 592126c

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Datetimelike
228228
- Bug in :class:`DateOffset` where attributes reconstructed from pickle files differ from original objects when input values exceed normal ranges (e.g months=12) (:issue:`34511`)
229229
- Bug in :meth:`DatetimeIndex.get_slice_bound` where ``datetime.date`` objects were not accepted or naive :class:`Timestamp` with a tz-aware :class:`DatetimeIndex` (:issue:`35690`)
230230
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
231+
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64`` or ``timedelta64`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`)
231232

232233
Timedelta
233234
^^^^^^^^^

pandas/core/arrays/datetimelike.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,8 @@ def _validate_searchsorted_value(self, value):
862862
# TODO: cast_str? we accept it for scalar
863863
value = self._validate_listlike(value, "searchsorted")
864864

865-
return self._unbox(value)
865+
rv = self._unbox(value)
866+
return self._rebox_native(rv)
866867

867868
def _validate_setitem_value(self, value):
868869
msg = (
@@ -941,9 +942,7 @@ def searchsorted(self, value, side="left", sorter=None):
941942
Array of insertion points with the same shape as `value`.
942943
"""
943944
value = self._validate_searchsorted_value(value)
944-
945-
# TODO: Use datetime64 semantics for sorting, xref GH#29844
946-
return self.asi8.searchsorted(value, side=side, sorter=sorter)
945+
return self._data.searchsorted(value, side=side, sorter=sorter)
947946

948947
def value_counts(self, dropna=False):
949948
"""

pandas/tests/arrays/test_datetimelike.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ def test_searchsorted(self):
241241
expected = np.array([2, 3], dtype=np.intp)
242242
tm.assert_numpy_array_equal(result, expected)
243243

244-
# Following numpy convention, NaT goes at the beginning
245-
# (unlike NaN which goes at the end)
244+
# GH#29884 match numpy convention on whether NaT goes
245+
# at the end or the beginning
246246
result = arr.searchsorted(pd.NaT)
247-
assert result == 0
247+
if _np_version_under1p18 or self.array_cls is PeriodArray:
248+
# Following numpy convention, NaT goes at the beginning
249+
# (unlike NaN which goes at the end)
250+
assert result == 0
251+
else:
252+
assert result == 10
248253

249254
def test_getitem_2d(self, arr1d):
250255
# 2d slicing on a 1D array

0 commit comments

Comments
 (0)