MCPcopy
hub / github.com/pandas-dev/pandas / asof

Method asof

pandas/core/indexes/base.py:5731–5803  ·  view source on GitHub ↗

Return the label from the index, or, if not present, the previous one. Assuming that the index is sorted, return the passed index label if it is in the index, or return the previous index label if the passed one is not in the index. Parameters -----

(self, label)

Source from the content-addressed store, hash-verified

5729
5730 @final
5731 def asof(self, label):
5732 """
5733 Return the label from the index, or, if not present, the previous one.
5734
5735 Assuming that the index is sorted, return the passed index label if it
5736 is in the index, or return the previous index label if the passed one
5737 is not in the index.
5738
5739 Parameters
5740 ----------
5741 label : object
5742 The label up to which the method returns the latest index label.
5743
5744 Returns
5745 -------
5746 object
5747 The passed label if it is in the index. The previous label if the
5748 passed label is not in the sorted index or `NaN` if there is no
5749 such label.
5750
5751 See Also
5752 --------
5753 Series.asof : Return the latest value in a Series up to the
5754 passed index.
5755 merge_asof : Perform an asof merge (similar to left join but it
5756 matches on nearest key rather than equal key).
5757 Index.get_loc : An `asof` is a thin wrapper around `get_loc`
5758 with method='pad'.
5759
5760 Examples
5761 --------
5762 `Index.asof` returns the latest index label up to the passed label.
5763
5764 >>> idx = pd.Index(["2013-12-31", "2014-01-02", "2014-01-03"])
5765 >>> idx.asof("2014-01-01")
5766 '2013-12-31'
5767
5768 If the label is in the index, the method returns the passed label.
5769
5770 >>> idx.asof("2014-01-02")
5771 '2014-01-02'
5772
5773 If all of the labels in the index are later than the passed label,
5774 NaN is returned.
5775
5776 >>> idx.asof("1999-01-02")
5777 nan
5778
5779 If the index is not sorted, an error is raised.
5780
5781 >>> idx_not_sorted = pd.Index(["2013-12-31", "2015-01-02", "2014-01-03"])
5782 >>> idx_not_sorted.asof("2013-12-31")
5783 Traceback (most recent call last):
5784 ValueError: index must be monotonic increasing or decreasing
5785 """
5786 self._searchsorted_monotonic(label) # validate sortedness
5787 try:
5788 loc = self.get_loc(label)

Callers 1

Calls 4

get_locMethod · 0.95
get_indexerMethod · 0.95
itemMethod · 0.45

Tested by 1