Skip to content

DOC: update the pd.Index.asof docstring #20344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 3 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
54 changes: 50 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,12 +2378,58 @@ def identical(self, other):

def asof(self, label):
"""
For a sorted index, return the most recent label up to and including
the passed label. Return NaN if not found.
Return the label from the index, or, if not present, the previous one.

See also
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
----------
label : object
The label up to which the method returns the latest index label.

Returns
-------
object : The passed label if it is in the index. The previous label
if the passed label is not in the sorted index, or NaN if there
is no such label.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just one minor formatting thing. The description goes to the next line, and the colon is not required. Also you can add backticks around the NaN. For the rest is perfect, really like this final result.


See Also
--------
get_loc : asof is a thin wrapper around get_loc with method='pad'
Series.asof : Return the latest value in a Series up to the
passed index.
merge_asof : Perform an asof merge (similar to left join but it
matches on nearest key rather than equal key).
Index.get_loc : `asof` is a thin wrapper around `get_loc`
with method='pad'.

Examples
--------
`Index.asof` returns the latest index label up to the passed label.

>>> idx = pd.Index(['2013-12-31', '2014-01-02', '2014-01-03'])
>>> idx.asof('2014-01-01')
'2013-12-31'

If the label is in the index, the method returns the passed label.

>>> idx.asof('2014-01-02')
'2014-01-02'

If all of the labels in the index are later than the passed label,
NaN is returned.

>>> idx.asof('1999-01-02')
nan

If the index is not sorted, an error is raised.

>>> idx_not_sorted = pd.Index(['2013-12-31', '2015-01-02',
... '2014-01-03'])
>>> idx_not_sorted.asof('2013-12-31')
Traceback (most recent call last):
ValueError: index must be monotonic increasing or decreasing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. The only think that comes to my mind is if it'd make sense to use dates in the examples. I think the example with integers is simpler, and easier to understand. But I'd say in practice the method is only used with dates.

Like, if you want to know the price of a stock, and you say, give me the price asof 1st of January. The 1st of January the markets are closed, so the price returned would be the one of the 31st of December or the last day the markets were open.

I'm not quite sure what would be the best. If you think you can improve the docstring with the user case I just mentioned, that would be great. Otherwise looks good to me.

And may be we could also add Series.asof and pandas.merge_asof to the See Also section.

Thanks for the good work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I've updated the docs using your suggestions.

"""
try:
loc = self.get_loc(label, method='pad')
Expand Down