Skip to content

Commit 1c7e0ab

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
searchsorted numpy compat for Period dtype (pandas-dev#36254)
1 parent dcfdd8e commit 1c7e0ab

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +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`)
231+
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64``, ``timedelta64`` or ``Period`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`,:issue:`36254`)
232232

233233
Timedelta
234234
^^^^^^^^^

pandas/core/arrays/period.py

+7
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ def astype(self, dtype, copy: bool = True):
587587
return self.asfreq(dtype.freq)
588588
return super().astype(dtype, copy=copy)
589589

590+
def searchsorted(self, value, side="left", sorter=None):
591+
value = self._validate_searchsorted_value(value).view("M8[ns]")
592+
593+
# Cast to M8 to get datetime-like NaT placement
594+
m8arr = self._ndarray.view("M8[ns]")
595+
return m8arr.searchsorted(value, side=side, sorter=sorter)
596+
590597
# ------------------------------------------------------------------
591598
# Arithmetic Methods
592599

pandas/tests/arrays/test_datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_searchsorted(self):
244244
# GH#29884 match numpy convention on whether NaT goes
245245
# at the end or the beginning
246246
result = arr.searchsorted(pd.NaT)
247-
if np_version_under1p18 or self.array_cls is PeriodArray:
247+
if np_version_under1p18:
248248
# Following numpy convention, NaT goes at the beginning
249249
# (unlike NaN which goes at the end)
250250
assert result == 0

pandas/tests/indexes/period/test_searchsorted.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
from pandas._libs.tslibs import IncompatibleFrequency
5+
from pandas.compat.numpy import np_version_under1p18
56

67
from pandas import NaT, Period, PeriodIndex, Series, array
78
import pandas._testing as tm
@@ -21,7 +22,13 @@ def test_searchsorted(self, freq):
2122
p2 = Period("2014-01-04", freq=freq)
2223
assert pidx.searchsorted(p2) == 3
2324

24-
assert pidx.searchsorted(NaT) == 0
25+
if np_version_under1p18:
26+
# GH#36254
27+
# Following numpy convention, NaT goes at the beginning
28+
# (unlike NaN which goes at the end)
29+
assert pidx.searchsorted(NaT) == 0
30+
else:
31+
assert pidx.searchsorted(NaT) == 5
2532

2633
msg = "Input has different freq=H from PeriodArray"
2734
with pytest.raises(IncompatibleFrequency, match=msg):

0 commit comments

Comments
 (0)