pby.html)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.asof  #

DataFrame.  asof  (  _ where  _ , _ subset  =  None  _ )  [ [source]
](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/generic.py#L8521-L8688) #

    

Return the last row(s) without any NaNs before  where  .

The last row (for each element in  where  , if list) without any NaN is taken.
In case of a [ ` DataFrame  ` ](pandas.DataFrame.html#pandas.DataFrame
"pandas.DataFrame") , the last row without NaN considering only the subset of
columns (if not  None  )

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

Parameters  :

    

**where** date or array-like of dates

    

Date(s) before which the last row(s) are returned.

**subset** str or array-like of str, default  None

    

For DataFrame, if not  None  , only use these columns to check for NaNs.

Returns  :

    

scalar, Series, or DataFrame

    

The return can be:

  * scalar : when  self  is a Series and  where  is a scalar 

  * Series: when  self  is a Series and  where  is an array-like, or when  self  is a DataFrame and  where  is a scalar 

  * DataFrame : when  self  is a DataFrame and  where  is an array-like 

See also

[ ` merge_asof  ` ](pandas.merge_asof.html#pandas.merge_asof
"pandas.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 scalar  where  .

    
    
    >>> s = pd.Series([1, 2, np.nan, 4], index=[10, 20, 30, 40])
    >>> s
    10    1.0
    20    2.0
    30    NaN
    40    4.0
    dtype: float64
    
    
    
    >>> s.asof(20)
    2.0
    

For a sequence  where  , a Series is returned. The first value is NaN, because
the first element of  where  is before the first index value.

    
    
    >>> s.asof([5, 20])
    5     NaN
    20    2.0
    dtype: float64
    

Missing values are not considered. The following is ` 2.0  ` , not NaN, even
though NaN is at the index location for ` 30  ` .

    
    
    >>> 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   b
    2018-02-27 09:03:30 NaN NaN
    2018-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   b
    2018-02-27 09:03:30  30.0 NaN
    2018-02-27 09:04:30  40.0 NaN
    

[ __ previous  pandas.DataFrame.asfreq  ](pandas.DataFrame.asfreq.html
"previous page") [ next  pandas.DataFrame.shift  __
](pandas.DataFrame.shift.html "next page")

__ On this page

  * ` DataFrame.asof()  `

[ __ Show Source ](../../_sources/reference/api/pandas.DataFrame.asof.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [