pandas.core.resample.Resampler.nearest#

final Resampler.nearest(limit=None)[源代码]#

使用最近的值进行重采样。

在重采样数据时,可能会出现缺失值(例如,当重采样频率高于原始频率时)。nearest 方法将用序列的最近成员的值替换重采样数据中出现的 NaN 值,该值基于索引值。原始数据中存在的缺失值不会被修改。如果给定了 limit,则仅为每个原始值填充此数量的值(在两个方向上)。

Parameters:
<strong>limit</strong>int, optional

要填充的最大值数量。

Returns:
Series 或 DataFrame

一个已用其最近值填充 NaN 值的上采样 Series 或 DataFrame。

参见

backfill

向后填充重采样数据中的新缺失值。

pad

向前填充 NaN 值。

Examples

>>> s = pd.Series([1, 2],
...               index=pd.date_range('20180101',
...                                   periods=2,
...                                   freq='1h'))
>>> s
2018-01-01 00:00:00    1
2018-01-01 01:00:00    2
Freq: h, dtype: int64
>>> s.resample('15min').nearest()
2018-01-01 00:00:00    1
2018-01-01 00:15:00    1
2018-01-01 00:30:00    2
2018-01-01 00:45:00    2
2018-01-01 01:00:00    2
Freq: 15min, dtype: int64

限制用于填充最近值的上采样数量:

>>> s.resample('15min').nearest(limit=1)
2018-01-01 00:00:00    1.0
2018-01-01 00:15:00    1.0
2018-01-01 00:30:00    NaN
2018-01-01 00:45:00    2.0
2018-01-01 01:00:00    2.0
Freq: 15min, dtype: float64