Skip to content

Series as of docstring #20226

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

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
57 changes: 38 additions & 19 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5401,40 +5401,59 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,

def asof(self, where, subset=None):
"""
The last row without any NaN is taken (or the last row without
NaN considering only the subset of columns in the case of a DataFrame)

.. versionadded:: 0.19.0 For DataFrame

If there is no good value, NaN is returned for a Series
a Series of NaN values for a DataFrame

Take the last row without any NaN.

Or the last row without NaN considering only the subset of columns in the case of a DataFrame.
If there is no matched value, NaN is returned for a Series a Series of NaN values for a DataFrame.

.. versionadded:: 0.19.0 For DataFrame.

Parameters
----------
where : date or array of dates
subset : string or list of strings, default None
if not None use these columns for NaN propagation
where : datetime
Date or array of dates.
subset : str
Copy link
Contributor

Choose a reason for hiding this comment

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

subset: str or list of strings
if not None, use the columns for NaN propagation.

String or list of strings, default None if not None use these columns for NaN propagation.

Notes
-----
Dates are assumed to be sorted
Raises if this is not the case
Dates are assumed to be sorted.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make a Raises section and add ValueError there (if dates are not sorted)

Raises if this is not the case.

Returns
-------
where is scalar

- value or NaN if input is Series
- Series if input is DataFrame
- value or NaN if input is Series.
- Series if input is DataFrame.

where is Index: same shape object as input
where is Index: same shape object as input.

See Also
--------
merge_asof

"""
merge_asof : Perform an asof merge. This is similar to a left-join except that we match on nearest key rather than equal keys.

Examples
--------
>>> import pandas as pd
Copy link
Contributor

Choose a reason for hiding this comment

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

don't need pandas import

>>> from datetime import datetime

>>> names = ['curupira', 'saci', 'boitata', 'tupa', float('NaN'), 'cuca']
>>> dates = [datetime.strptime('20130101', '%Y%m%d'),
... datetime.strptime('20140101', '%Y%m%d'),
... datetime.strptime('20140101', '%Y%m%d'),
... datetime.strptime('20150101', '%Y%m%d'),
... datetime.strptime('20150101', '%Y%m%d'),
... datetime.strptime('20160101', '%Y%m%d')]
Copy link
Member

Choose a reason for hiding this comment

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

you can generate some dates with pd.date_range (if you want a regular index), or pd.to_datetime.

>>> mySeries = pd.Series(data=names, index=dates)
Copy link
Member

Choose a reason for hiding this comment

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

you can call the series s

>>> mySeries.asof(datetime.strptime('20150102', '%Y%m%d'))
Copy link
Member

Choose a reason for hiding this comment

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

Can you use pd.Timestamp("2015-01-02") instead? (to use the pandas equivalent way)

'tupa'
>>> mySeries.asof(datetime.strptime('20190201', '%Y%m%d'))
'cuca'
>>> mySeries.asof(datetime.strptime('20120201', '%Y%m%d'))
nan
"""

if isinstance(where, compat.string_types):
from pandas import to_datetime
where = to_datetime(where)
Expand Down