Return the locations (indices) of labels in the index. As in the :meth:`pandas.Index.asof`, if the label (a particular entry in ``where``) is not in the index, the latest index label up to the passed label is chosen and its index returned. If all of the lab
(
self, where: Index, mask: npt.NDArray[np.bool_]
)
| 5803 | return self[loc] |
| 5804 | |
| 5805 | def asof_locs( |
| 5806 | self, where: Index, mask: npt.NDArray[np.bool_] |
| 5807 | ) -> npt.NDArray[np.intp]: |
| 5808 | """ |
| 5809 | Return the locations (indices) of labels in the index. |
| 5810 | |
| 5811 | As in the :meth:`pandas.Index.asof`, if the label (a particular entry in |
| 5812 | ``where``) is not in the index, the latest index label up to the |
| 5813 | passed label is chosen and its index returned. |
| 5814 | |
| 5815 | If all of the labels in the index are later than a label in ``where``, |
| 5816 | -1 is returned. |
| 5817 | |
| 5818 | ``mask`` is used to ignore ``NA`` values in the index during calculation. |
| 5819 | |
| 5820 | Parameters |
| 5821 | ---------- |
| 5822 | where : Index |
| 5823 | An Index consisting of an array of timestamps. |
| 5824 | mask : np.ndarray[bool] |
| 5825 | Array of booleans denoting where values in the original |
| 5826 | data are not ``NA``. |
| 5827 | |
| 5828 | Returns |
| 5829 | ------- |
| 5830 | np.ndarray[np.intp] |
| 5831 | An array of locations (indices) of the labels from the index |
| 5832 | which correspond to the return values of :meth:`pandas.Index.asof` |
| 5833 | for every element in ``where``. |
| 5834 | |
| 5835 | See Also |
| 5836 | -------- |
| 5837 | Index.asof : Return the label from the index, or, if not present, the |
| 5838 | previous one. |
| 5839 | |
| 5840 | Examples |
| 5841 | -------- |
| 5842 | >>> idx = pd.date_range("2023-06-01", periods=3, freq="D") |
| 5843 | >>> where = pd.DatetimeIndex( |
| 5844 | ... ["2023-05-30 00:12:00", "2023-06-01 00:00:00", "2023-06-02 23:59:59"] |
| 5845 | ... ) |
| 5846 | >>> mask = np.ones(3, dtype=bool) |
| 5847 | >>> idx.asof_locs(where, mask) |
| 5848 | array([-1, 0, 1]) |
| 5849 | |
| 5850 | We can use ``mask`` to ignore certain values in the index during calculation. |
| 5851 | |
| 5852 | >>> mask[1] = False |
| 5853 | >>> idx.asof_locs(where, mask) |
| 5854 | array([-1, 0, 0]) |
| 5855 | """ |
| 5856 | # error: No overload variant of "searchsorted" of "ndarray" matches argument |
| 5857 | # types "Union[ExtensionArray, ndarray[Any, Any]]", "str" |
| 5858 | # TODO: will be fixed when ExtensionArray.searchsorted() is fixed |
| 5859 | locs = self._values[mask].searchsorted( |
| 5860 | where._values, |
| 5861 | side="right", # type: ignore[call-overload] |
| 5862 | ) |