@@ -1632,6 +1632,58 @@ def repeat(self, repeats: int) -> "Index":
16321632 else :
16331633 return ks .concat ([kdf ] * repeats ).index
16341634
1635+ def asof (self , label ):
1636+ """
1637+ Return the label from the index, or, if not present, the previous one.
1638+
1639+ Assuming that the index is sorted, return the passed index label if it
1640+ is in the index, or return the previous index label if the passed one
1641+ is not in the index.
1642+
1643+ .. note:: This API is dependent on :meth:`Index.is_monotonic_increasing`
1644+ which can be expensive.
1645+
1646+ Parameters
1647+ ----------
1648+ label : object
1649+ The label up to which the method returns the latest index label.
1650+
1651+ Returns
1652+ -------
1653+ object
1654+ The passed label if it is in the index. The previous label if the
1655+ passed label is not in the sorted index or `NaN` if there is no
1656+ such label.
1657+
1658+ Examples
1659+ --------
1660+ `Index.asof` returns the latest index label up to the passed label.
1661+
1662+ >>> idx = ks.Index(['2013-12-31', '2014-01-02', '2014-01-03'])
1663+ >>> idx.asof('2014-01-01')
1664+ '2013-12-31'
1665+
1666+ If the label is in the index, the method returns the passed label.
1667+
1668+ >>> idx.asof('2014-01-02')
1669+ '2014-01-02'
1670+
1671+ If all of the labels in the index are later than the passed label,
1672+ NaN is returned.
1673+
1674+ >>> idx.asof('1999-01-02')
1675+ nan
1676+ """
1677+ sdf = self ._internal ._sdf
1678+ if self .is_monotonic_increasing :
1679+ sdf = sdf .select (self ._scol ).where (self ._scol <= label ).select (F .max (self ._scol ))
1680+ elif self .is_monotonic_decreasing :
1681+ sdf = sdf .select (self ._scol ).where (self ._scol >= label ).select (F .min (self ._scol ))
1682+ else :
1683+ raise ValueError ("index must be monotonic increasing or decreasing" )
1684+ result = sdf .head ()[0 ]
1685+ return result if result is not None else np .nan
1686+
16351687 def union (self , other , sort = None ):
16361688 """
16371689 Form the union of two Index objects.
@@ -2366,6 +2418,11 @@ def argmax(self):
23662418 def argmin (self ):
23672419 raise TypeError ("reduction operation 'argmin' not allowed for this dtype" )
23682420
2421+ def asof (self , label ):
2422+ raise NotImplementedError (
2423+ "only the default get_loc method is currently supported for MultiIndex"
2424+ )
2425+
23692426 @property
23702427 def is_all_dates (self ):
23712428 """
0 commit comments