pandas.Series.asof#

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

返回 where 之前最后一个不含 NaN 的行(或行)。

获取最后一个没有 NaN 的行(对于 where 中的每个元素,如果为列表)。对于 DataFrame ,则获取考虑了(如果 None 则不考虑)列子集后最后一个没有 NaN 的行。

如果不存在有效值,则返回 Series 的 NaN 或 DataFrame 的 NaN 值 Series

Parameters:
where日期或日期数组

返回该日期之前的最后一行(多行)。

subset : 字符串或字符串数组,默认 None字符串或字符串数组,默认

对于 DataFrame,如果不是 None,则仅使用这些列来检查 NaNs。

Returns:
标量、Series 或 DataFrame

返回值可以是:

  • 标量:当 self 是 Series 且 where 是标量时

  • Series:当 self 是 Series 且 where 是数组时,或者当 self 是 DataFrame 且 where 是标量时

  • DataFrame:当 self 是 DataFrame 且 where 是数组时

参见

merge_asof

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

Notes

假设日期已排序。如果不是这种情况,则引发错误。

Examples

一个 Series 和一个标量 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,将返回一个 Series。第一个值为 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.0 NaN
2018-02-27 09:04:30  40.0 NaN