pandas.Index.asof#

final Index.asof(label)[源代码]#

返回索引中的标签,如果不存在,则返回前一个。

假设索引已排序,如果传入的索引标签存在于索引中,则返回该标签,否则返回前一个索引标签。

Parameters:
labelobject

方法返回该索引标签到哪个位置。

Returns:
object

如果标签存在于索引中,则返回传入的标签。如果传入的标签不在排序的索引中,则返回前一个标签,或者如果不存在这样的标签,则返回 NaN

参见

Series.asof

返回 Series 中截至传入索引的最新值。

merge_asof

执行 asof 合并(类似于左连接,但它匹配最近的键而不是相等的键)。

Index.get_loc

Index.asofget_loc 的一个非常简单的包装器,其中 method=’pad’。

Examples

Index.asof 返回截至传入标签的最新索引标签。

>>> idx = pd.Index(['2013-12-31', '2014-01-02', '2014-01-03'])
>>> idx.asof('2014-01-01')
'2013-12-31'

如果标签存在于索引中,则方法返回传入的标签。

>>> idx.asof('2014-01-02')
'2014-01-02'

如果索引中的所有标签都晚于传入的标签,则返回 NaN。

>>> idx.asof('1999-01-02')
nan

如果索引未排序,则会引发错误。

>>> idx_not_sorted = pd.Index(['2013-12-31', '2015-01-02',
...                            '2014-01-03'])
>>> idx_not_sorted.asof('2013-12-31')
Traceback (most recent call last):
ValueError: index must be monotonic increasing or decreasing