LoginSignup
6

More than 5 years have passed since last update.

(Pandas) 時系列データの隙間を埋める

Posted at

例えば以下のような、DatetimeIndex を持つ Series データがあったとする。

In [1]: ts

ymd
2016-03-23    132
2016-03-25    231
2016-03-28     56
Name: ts, dtype: int64

日付が飛び飛びになっているので、この隙間を 0 で埋めたい。

このような場合は、pandas.Series.asfreq を使うと埋めることができる。

In [2]: ts.asfreq('d', fill_value=0)

ymd
2016-03-23    132
2016-03-24      0
2016-03-25    231
2016-03-26      0
2016-03-27      0
2016-03-28     56
Freq: D, Name: ts, dtype: int64

例では Series オブジェクトだったが、DataFrame に対しても同じことができる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6