Skip to content

Commit 3dd0920

Browse files
DataOmbudsmanmroeschke
authored andcommitted
DOC: update the pd.Index.asof docstring (#20344)
* DOC: update the pd.Index.asof docstring * clearer descriptions, restructured examples. * extended See Also section, dates instead of integers in Examples * minor formatting
1 parent 07d1d0a commit 3dd0920

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

pandas/core/indexes/base.py

+51-4
Original file line numberDiff line numberDiff line change
@@ -2364,12 +2364,59 @@ def identical(self, other):
23642364

23652365
def asof(self, label):
23662366
"""
2367-
For a sorted index, return the most recent label up to and including
2368-
the passed label. Return NaN if not found.
2367+
Return the label from the index, or, if not present, the previous one.
23692368
2370-
See also
2369+
Assuming that the index is sorted, return the passed index label if it
2370+
is in the index, or return the previous index label if the passed one
2371+
is not in the index.
2372+
2373+
Parameters
2374+
----------
2375+
label : object
2376+
The label up to which the method returns the latest index label.
2377+
2378+
Returns
2379+
-------
2380+
object
2381+
The passed label if it is in the index. The previous label if the
2382+
passed label is not in the sorted index or `NaN` if there is no
2383+
such label.
2384+
2385+
See Also
23712386
--------
2372-
get_loc : asof is a thin wrapper around get_loc with method='pad'
2387+
Series.asof : Return the latest value in a Series up to the
2388+
passed index.
2389+
merge_asof : Perform an asof merge (similar to left join but it
2390+
matches on nearest key rather than equal key).
2391+
Index.get_loc : `asof` is a thin wrapper around `get_loc`
2392+
with method='pad'.
2393+
2394+
Examples
2395+
--------
2396+
`Index.asof` returns the latest index label up to the passed label.
2397+
2398+
>>> idx = pd.Index(['2013-12-31', '2014-01-02', '2014-01-03'])
2399+
>>> idx.asof('2014-01-01')
2400+
'2013-12-31'
2401+
2402+
If the label is in the index, the method returns the passed label.
2403+
2404+
>>> idx.asof('2014-01-02')
2405+
'2014-01-02'
2406+
2407+
If all of the labels in the index are later than the passed label,
2408+
NaN is returned.
2409+
2410+
>>> idx.asof('1999-01-02')
2411+
nan
2412+
2413+
If the index is not sorted, an error is raised.
2414+
2415+
>>> idx_not_sorted = pd.Index(['2013-12-31', '2015-01-02',
2416+
... '2014-01-03'])
2417+
>>> idx_not_sorted.asof('2013-12-31')
2418+
Traceback (most recent call last):
2419+
ValueError: index must be monotonic increasing or decreasing
23732420
"""
23742421
try:
23752422
loc = self.get_loc(label, method='pad')

0 commit comments

Comments
 (0)