pandas.Series.asof#

Series.asof(where, subset=None)[源代码]#

返回之前没有任何NAN的最后一行 where

最后一行(对于 where ,如果列表),而不使用任何NAN。在以下情况下 DataFrame ,不带NaN的最后一行只考虑列子集(如果不是 None )

如果没有正确的值,则为DataFrame的一个系列或一系列NaN值返回NaN

参数
where日期或类似数组的日期

返回最后一行之前的日期。

子集 :字符串或字符串的类似数组,默认 None字符串或类似数组字符串,默认

对于DataFrame,如果不是 None ,仅使用这些列检查是否有NAN。

退货
标量、系列或DataFrame

回报可以是:

  • 标量:何时 self 是一部系列片 where 是一个标量

  • 系列:何时 self 是一部系列片 where 是类似数组的,或者当 self 是一个DataFrame,并且 where 是一个标量

  • DataFrame:何时 self 是一个DataFrame,并且 where 是一个类似数组的

返回标量、系列或DataFrame。

参见

merge_asof

执行ASOF合并。类似于左连接。

注意事项

假定日期已排序。如果不是这样,则引发。

示例

一个级数和一个标量 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

对于序列 where ,则返回一个系列。第一个值是NaN,因为 where 在第一个索引值之前。

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

不考虑缺少的值。以下是 2.0 ,而不是NaN,即使NaN位于的索引位置 30

>>> s.asof(30)
2.0

将所有列都考虑在内

>>> 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

只考虑一列

>>> 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 NaN
2018-02-27 09:04:30  40 NaN