Skip to content

COMPAT: match numpy behavior for searchsorted on dt64/td64 #36176

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 8, 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 @@ -228,6 +228,7 @@ Datetimelike
- 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`)
- 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`)
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)
- 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`)

Timedelta
^^^^^^^^^
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ def _validate_searchsorted_value(self, value):
# TODO: cast_str? we accept it for scalar
value = self._validate_listlike(value, "searchsorted")

return self._unbox(value)
rv = self._unbox(value)
return self._rebox_native(rv)

def _validate_setitem_value(self, value):
msg = (
Expand Down Expand Up @@ -937,9 +938,7 @@ def searchsorted(self, value, side="left", sorter=None):
Array of insertion points with the same shape as `value`.
"""
value = self._validate_searchsorted_value(value)

# TODO: Use datetime64 semantics for sorting, xref GH#29844
return self.asi8.searchsorted(value, side=side, sorter=sorter)
return self._data.searchsorted(value, side=side, sorter=sorter)

def value_counts(self, dropna=False):
"""
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,15 @@ def test_searchsorted(self):
expected = np.array([2, 3], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
# GH#29884 match numpy convention on whether NaT goes
# at the end or the beginning
result = arr.searchsorted(pd.NaT)
assert result == 0
if _np_version_under1p18 or self.array_cls is PeriodArray:
# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
assert result == 0
else:
assert result == 10

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