Skip to content
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
6 changes: 3 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4490,7 +4490,7 @@ def asof(self, label):
loc = loc.indices(len(self))[-1]
return self[loc]

def asof_locs(self, where, mask):
def asof_locs(self, where: "Index", mask) -> np.ndarray:
"""
Return the locations (indices) of labels in the index.

Expand Down Expand Up @@ -4518,13 +4518,13 @@ def asof_locs(self, where, mask):
which correspond to the return values of the `asof` function
for every element in `where`.
"""
locs = self.values[mask].searchsorted(where.values, side="right")
locs = self._values[mask].searchsorted(where._values, side="right")
locs = np.where(locs > 0, locs - 1, 0)

result = np.arange(len(self))[mask].take(locs)

first = mask.argmax()
result[(locs == 0) & (where.values < self.values[first])] = -1
result[(locs == 0) & (where._values < self._values[first])] = -1

return result

Expand Down
21 changes: 5 additions & 16 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,28 +404,17 @@ def __array_wrap__(self, result, context=None):
# cannot pass _simple_new as it is
return type(self)(result, freq=self.freq, name=self.name)

def asof_locs(self, where, mask: np.ndarray) -> np.ndarray:
def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
"""
where : array of timestamps
mask : array of booleans where data is not NA
"""
where_idx = where
if isinstance(where_idx, DatetimeIndex):
where_idx = PeriodIndex(where_idx._values, freq=self.freq)
elif not isinstance(where_idx, PeriodIndex):
if isinstance(where, DatetimeIndex):
where = PeriodIndex(where._values, freq=self.freq)
elif not isinstance(where, PeriodIndex):
raise TypeError("asof_locs `where` must be DatetimeIndex or PeriodIndex")
elif where_idx.freq != self.freq:
raise raise_on_incompatible(self, where_idx)

locs = self.asi8[mask].searchsorted(where_idx.asi8, side="right")

locs = np.where(locs > 0, locs - 1, 0)
result = np.arange(len(self))[mask].take(locs)

first = mask.argmax()
result[(locs == 0) & (where_idx.asi8 < self.asi8[first])] = -1

return result
return super().asof_locs(where, mask)

@doc(Index.astype)
def astype(self, dtype, copy: bool = True, how="start"):
Expand Down