pandas.Index.asof_locs#

Index.asof_locs(where, mask)[源代码]#

返回索引中标签的位置(索引)。

pandas.Index.asof() 类似,如果标签(where 中的特定条目)不存在于索引中,则选择最新的索引标签(截至传入标签)并返回其索引。

如果索引中的所有标签都晚于 where 中的某个标签,则返回 -1。

mask 用于在计算过程中忽略索引中的 NA 值。

Parameters:
wherepandas.DataFrame.keys

由时间戳数组组成的 Index。

masknp.ndarray[bool]

布尔数组,表示原始数据中的哪些值为非 NA

Returns:
np.ndarray[np.intp]

一个由索引中的标签位置(索引)组成的数组,这些标签对应于对 where 中的每个元素调用 pandas.Index.asof() 的返回值。

参见

Index.asof

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

Examples

>>> idx = pd.date_range('2023-06-01', periods=3, freq='D')
>>> where = pd.DatetimeIndex(['2023-05-30 00:12:00', '2023-06-01 00:00:00',
...                           '2023-06-02 23:59:59'])
>>> mask = np.ones(3, dtype=bool)
>>> idx.asof_locs(where, mask)
array([-1,  0,  1])

我们可以使用 mask 在计算过程中忽略索引中的某些值。

>>> mask[1] = False
>>> idx.asof_locs(where, mask)
array([-1,  0,  0])