Note

Pandas (31) 본문

Pandas

Pandas (31)

알 수 없는 사용자 2022. 8. 23. 22:04
728x90

How to fill an intermittent time series so all missing dates show up with values of previous non-missing date?

# Input
ser = pd.Series([1,10,3, np.nan], index=pd.to_datetime(['2000-01-01', '2000-01-03', '2000-01-06', '2000-01-08']))

# 1
ser.resample('D').ffill()  # fill with previous value

# 2
ser.resample('D').bfill()  # fill with next value
ser.resample('D').bfill().ffill()  # fill next else prev value

# output
2000-01-01     1.0
2000-01-02    10.0
2000-01-03    10.0
2000-01-04     3.0
2000-01-05     3.0
2000-01-06     3.0
2000-01-07     3.0
2000-01-08     3.0
Freq: D, dtype: float64

'Pandas' 카테고리의 다른 글

Pandas (33)  (0) 2022.08.25
Pandas (32)  (0) 2022.08.24
Pandas (30)  (0) 2022.08.22
Pandas (29)  (0) 2022.08.21
Pandas (28)  (0) 2022.08.20
Comments