- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.asof#
- DataFrame.asof(where,subset=None)[source]#
Return the last row(s) without any NaNs beforewhere.
The last row (for each element inwhere, if list) without anyNaN is taken.In case of a
DataFrame
, the last row without NaNconsidering only the subset of columns (if notNone)If there is no good value, NaN is returned for a Series ora Series of NaN values for a DataFrame
- Parameters:
- wheredate or array-like of dates
Date(s) before which the last row(s) are returned.
- subsetstr or array-like of str, defaultNone
For DataFrame, if notNone, only use these columns tocheck for NaNs.
- Returns:
- scalar, Series, or DataFrame
The return can be:
scalar : whenself is a Series andwhere is a scalar
Series: whenself is a Series andwhere is an array-like,or whenself is a DataFrame andwhere is a scalar
DataFrame : whenself is a DataFrame andwhere is anarray-like
See also
merge_asof
Perform an asof merge. Similar to left join.
Notes
Dates are assumed to be sorted. Raises if this is not the case.
Examples
A Series and a scalarwhere.
>>>s=pd.Series([1,2,np.nan,4],index=[10,20,30,40])>>>s10 1.020 2.030 NaN40 4.0dtype: float64
>>>s.asof(20)2.0
For a sequencewhere, a Series is returned. The first value isNaN, because the first element ofwhere is before the firstindex value.
>>>s.asof([5,20])5 NaN20 2.0dtype: float64
Missing values are not considered. The following is
2.0
, notNaN, even though NaN is at the index location for30
.>>>s.asof(30)2.0
Take all columns into consideration
>>>df=pd.DataFrame({'a':[10.,20.,30.,40.,50.],...'b':[None,None,None,None,500]},...index=pd.DatetimeIndex(['2018-02-27 09:01:00',...'2018-02-27 09:02:00',...'2018-02-27 09:03:00',...'2018-02-27 09:04:00',...'2018-02-27 09:05:00']))>>>df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',...'2018-02-27 09:04:30'])) a b2018-02-27 09:03:30 NaN NaN2018-02-27 09:04:30 NaN NaN
Take a single column into consideration
>>>df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',...'2018-02-27 09:04:30']),...subset=['a']) a b2018-02-27 09:03:30 30.0 NaN2018-02-27 09:04:30 40.0 NaN