用法:
Resampler.fillna(method, limit=None)
填充上采樣引入的缺失值。
在統計學中,插補是用替換值 [1] 替換缺失數據的過程。重采樣數據時,可能會出現缺失值(例如,重采樣頻率高於原始頻率時)。
原始數據中存在的缺失值不會被修改。
- method:{‘pad’, ‘backfill’, ‘ffill’, ‘bfill’, ‘nearest’}
用於填充重采樣數據中的空洞的方法
‘pad’ or ‘ffill’:使用先前的有效觀察來填補空白(前向填充)。
‘backfill’ or ‘bfill’:使用下一個有效觀察來填補空白。
‘nearest’:使用最近的有效觀察來填補空白。
- limit:int 可選
要填充多少個連續缺失值的限製。
- 係列或DataFrame
填充缺失值的上采樣係列或 DataFrame。
參數:
返回:
參考:
例子:
重采樣係列:
>>> s = pd.Series([1, 2, 3], ... index=pd.date_range('20180101', periods=3, freq='h')) >>> s 2018-01-01 00:00:00 1 2018-01-01 01:00:00 2 2018-01-01 02:00:00 3 Freq: H, dtype: int64
在不填充缺失值的情況下,您會得到:
>>> s.resample("30min").asfreq() 2018-01-01 00:00:00 1.0 2018-01-01 00:30:00 NaN 2018-01-01 01:00:00 2.0 2018-01-01 01:30:00 NaN 2018-01-01 02:00:00 3.0 Freq: 30T, dtype: float64
>>> s.resample('30min').fillna("backfill") 2018-01-01 00:00:00 1 2018-01-01 00:30:00 2 2018-01-01 01:00:00 2 2018-01-01 01:30:00 3 2018-01-01 02:00:00 3 Freq: 30T, dtype: int64
>>> s.resample('15min').fillna("backfill", limit=2) 2018-01-01 00:00:00 1.0 2018-01-01 00:15:00 NaN 2018-01-01 00:30:00 2.0 2018-01-01 00:45:00 2.0 2018-01-01 01:00:00 2.0 2018-01-01 01:15:00 NaN 2018-01-01 01:30:00 3.0 2018-01-01 01:45:00 3.0 2018-01-01 02:00:00 3.0 Freq: 15T, dtype: float64
>>> s.resample('30min').fillna("pad") 2018-01-01 00:00:00 1 2018-01-01 00:30:00 1 2018-01-01 01:00:00 2 2018-01-01 01:30:00 2 2018-01-01 02:00:00 3 Freq: 30T, dtype: int64
>>> s.resample('30min').fillna("nearest") 2018-01-01 00:00:00 1 2018-01-01 00:30:00 2 2018-01-01 01:00:00 2 2018-01-01 01:30:00 3 2018-01-01 02:00:00 3 Freq: 30T, dtype: int64
上采樣之前存在的缺失值不受影響。
>>> sm = pd.Series([1, None, 3], ... index=pd.date_range('20180101', periods=3, freq='h')) >>> sm 2018-01-01 00:00:00 1.0 2018-01-01 01:00:00 NaN 2018-01-01 02:00:00 3.0 Freq: H, dtype: float64
>>> sm.resample('30min').fillna('backfill') 2018-01-01 00:00:00 1.0 2018-01-01 00:30:00 NaN 2018-01-01 01:00:00 NaN 2018-01-01 01:30:00 3.0 2018-01-01 02:00:00 3.0 Freq: 30T, dtype: float64
>>> sm.resample('30min').fillna('pad') 2018-01-01 00:00:00 1.0 2018-01-01 00:30:00 1.0 2018-01-01 01:00:00 NaN 2018-01-01 01:30:00 NaN 2018-01-01 02:00:00 3.0 Freq: 30T, dtype: float64
>>> sm.resample('30min').fillna('nearest') 2018-01-01 00:00:00 1.0 2018-01-01 00:30:00 NaN 2018-01-01 01:00:00 NaN 2018-01-01 01:30:00 3.0 2018-01-01 02:00:00 3.0 Freq: 30T, dtype: float64
DataFrame 重采樣是按列進行的。所有相同的選項都可用。
>>> df = pd.DataFrame({'a': [2, np.nan, 6], 'b': [1, 3, 5]}, ... index=pd.date_range('20180101', periods=3, ... freq='h')) >>> df a b 2018-01-01 00:00:00 2.0 1 2018-01-01 01:00:00 NaN 3 2018-01-01 02:00:00 6.0 5
>>> df.resample('30min').fillna("bfill") a b 2018-01-01 00:00:00 2.0 1 2018-01-01 00:30:00 NaN 3 2018-01-01 01:00:00 NaN 3 2018-01-01 01:30:00 6.0 5 2018-01-01 02:00:00 6.0 5
相關用法
- Python pandas.core.resample.Resampler.nearest用法及代碼示例
- Python pandas.core.resample.Resampler.transform用法及代碼示例
- Python pandas.core.resample.Resampler.bfill用法及代碼示例
- Python pandas.core.resample.Resampler.apply用法及代碼示例
- Python pandas.core.resample.Resampler.backfill用法及代碼示例
- Python pandas.core.resample.Resampler.mean用法及代碼示例
- Python pandas.core.resample.Resampler.pipe用法及代碼示例
- Python pandas.core.resample.Resampler.interpolate用法及代碼示例
- Python pandas.core.resample.Resampler.aggregate用法及代碼示例
- Python pandas.core.groupby.GroupBy.nth用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.unique用法及代碼示例
- Python pandas.core.window.rolling.Window.mean用法及代碼示例
- Python pandas.core.groupby.DataFrameGroupBy.hist用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.nlargest用法及代碼示例
- Python pandas.core.window.expanding.Expanding.kurt用法及代碼示例
- Python pandas.core.window.expanding.Expanding.sum用法及代碼示例
- Python pandas.core.window.expanding.Expanding.median用法及代碼示例
- Python pandas.core.window.expanding.Expanding.std用法及代碼示例
- Python pandas.core.window.rolling.Window.std用法及代碼示例
- Python pandas.core.window.rolling.Rolling.aggregate用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.core.resample.Resampler.fillna。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。