Skip to content

Commit 8c29ede

Browse files
dragosthealexJustinZhengBC
authored andcommitted
DOC: Rephrased doc for Series.asof. Added examples (pandas-dev#21034)
1 parent ae938fd commit 8c29ede

File tree

1 file changed

+73
-15
lines changed

1 file changed

+73
-15
lines changed

pandas/core/generic.py

+73-15
Original file line numberDiff line numberDiff line change
@@ -6497,40 +6497,98 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
64976497

64986498
def asof(self, where, subset=None):
64996499
"""
6500-
The last row without any NaN is taken (or the last row without
6501-
NaN considering only the subset of columns in the case of a DataFrame)
6500+
Return the last row(s) without any `NaN`s before `where`.
6501+
6502+
The last row (for each element in `where`, if list) without any
6503+
`NaN` is taken.
6504+
In case of a :class:`~pandas.DataFrame`, the last row without `NaN`
6505+
considering only the subset of columns (if not `None`)
65026506
65036507
.. versionadded:: 0.19.0 For DataFrame
65046508
6505-
If there is no good value, NaN is returned for a Series
6509+
If there is no good value, `NaN` is returned for a Series or
65066510
a Series of NaN values for a DataFrame
65076511
65086512
Parameters
65096513
----------
6510-
where : date or array of dates
6511-
subset : string or list of strings, default None
6512-
if not None use these columns for NaN propagation
6514+
where : date or array-like of dates
6515+
Date(s) before which the last row(s) are returned.
6516+
subset : str or array-like of str, default `None`
6517+
For DataFrame, if not `None`, only use these columns to
6518+
check for `NaN`s.
65136519
65146520
Notes
65156521
-----
6516-
Dates are assumed to be sorted
6517-
Raises if this is not the case
6522+
Dates are assumed to be sorted. Raises if this is not the case.
65186523
65196524
Returns
65206525
-------
6521-
where is scalar
6522-
6523-
- value or NaN if input is Series
6524-
- Series if input is DataFrame
6526+
scalar, Series, or DataFrame
65256527
6526-
where is Index: same shape object as input
6528+
* scalar : when `self` is a Series and `where` is a scalar
6529+
* Series: when `self` is a Series and `where` is an array-like,
6530+
or when `self` is a DataFrame and `where` is a scalar
6531+
* DataFrame : when `self` is a DataFrame and `where` is an
6532+
array-like
65276533
65286534
See Also
65296535
--------
6530-
merge_asof
6536+
merge_asof : Perform an asof merge. Similar to left join.
65316537
6532-
"""
6538+
Examples
6539+
--------
6540+
A Series and a scalar `where`.
6541+
6542+
>>> s = pd.Series([1, 2, np.nan, 4], index=[10, 20, 30, 40])
6543+
>>> s
6544+
10 1.0
6545+
20 2.0
6546+
30 NaN
6547+
40 4.0
6548+
dtype: float64
6549+
6550+
>>> s.asof(20)
6551+
2.0
65336552
6553+
For a sequence `where`, a Series is returned. The first value is
6554+
``NaN``, because the first element of `where` is before the first
6555+
index value.
6556+
6557+
>>> s.asof([5, 20])
6558+
5 NaN
6559+
20 2.0
6560+
dtype: float64
6561+
6562+
Missing values are not considered. The following is ``2.0``, not
6563+
``NaN``, even though ``NaN`` is at the index location for ``30``.
6564+
6565+
>>> s.asof(30)
6566+
2.0
6567+
6568+
Take all columns into consideration
6569+
6570+
>>> df = pd.DataFrame({'a': [10, 20, 30, 40, 50],
6571+
... 'b': [None, None, None, None, 500]},
6572+
... index=pd.DatetimeIndex(['2018-02-27 09:01:00',
6573+
... '2018-02-27 09:02:00',
6574+
... '2018-02-27 09:03:00',
6575+
... '2018-02-27 09:04:00',
6576+
... '2018-02-27 09:05:00']))
6577+
>>> df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',
6578+
... '2018-02-27 09:04:30']))
6579+
a b
6580+
2018-02-27 09:03:30 NaN NaN
6581+
2018-02-27 09:04:30 NaN NaN
6582+
6583+
Take a single column into consideration
6584+
6585+
>>> df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',
6586+
... '2018-02-27 09:04:30']),
6587+
... subset=['a'])
6588+
a b
6589+
2018-02-27 09:03:30 30.0 NaN
6590+
2018-02-27 09:04:30 40.0 NaN
6591+
"""
65346592
if isinstance(where, compat.string_types):
65356593
from pandas import to_datetime
65366594
where = to_datetime(where)

0 commit comments

Comments
 (0)