Skip to content

searchsorted numpy compat for Period dtype #36254

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 3 commits into from
Sep 12, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +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`)
- 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`)

Timedelta
^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ def astype(self, dtype, copy: bool = True):
return self.asfreq(dtype.freq)
return super().astype(dtype, copy=copy)

def searchsorted(self, value, side="left", sorter=None):
value = self._validate_searchsorted_value(value).view("M8[ns]")

# Cast to M8 to get datetime-like NaT placement
m8arr = self._ndarray.view("M8[ns]")
return m8arr.searchsorted(value, side=side, sorter=sorter)

# ------------------------------------------------------------------
# Arithmetic Methods

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_searchsorted(self):
# GH#29884 match numpy convention on whether NaT goes
# at the end or the beginning
result = arr.searchsorted(pd.NaT)
if np_version_under1p18 or self.array_cls is PeriodArray:
if np_version_under1p18:
# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
assert result == 0
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/indexes/period/test_searchsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from pandas._libs.tslibs import IncompatibleFrequency
from pandas.compat.numpy import np_version_under1p18

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

assert pidx.searchsorted(NaT) == 0
if np_version_under1p18:
# GH#36254
# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
assert pidx.searchsorted(NaT) == 0
else:
assert pidx.searchsorted(NaT) == 5

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