日付に対してデータがない場合の欠損値をゼロで埋めてチャートをプロットする方法 

# CSVデータ

BEFORE:

```
ykishi@dezembro pandas-test % cat sample_date.csv 
date,apple,orange
2021-04-05,600,192
2021-04-06,5050,229
2021-04-09,50,2229
2021-04-11,50,4524
2021-04-15,0,8000
2021-04-21,450,5900
2021-04-25,4500,15900
2021-04-28,1,34
2021-04-29,300,155
2021-05-01,8999,6660
```
AFTER:

```
ykishi@dezembro pandas-test % cat resampled.csv   
date,apple,orange
2021-04-05,600,192
2021-04-06,5050,229
2021-04-07,0,0
2021-04-08,0,0
2021-04-09,50,2229
2021-04-10,0,0
2021-04-11,50,4524
2021-04-12,0,0
2021-04-13,0,0
2021-04-14,0,0
2021-04-15,0,8000
2021-04-16,0,0
2021-04-17,0,0
2021-04-18,0,0
2021-04-19,0,0
2021-04-20,0,0
2021-04-21,450,5900
2021-04-22,0,0
2021-04-23,0,0
2021-04-24,0,0
2021-04-25,4500,15900
2021-04-26,0,0
2021-04-27,0,0
2021-04-28,1,34
2021-04-29,300,155
2021-04-30,0,0
2021-05-01,8999,6660
```

# ソースコード

```
ykishi@dezembro pandas-test % cat resample_test.py
#!/usr/bin/env python3

import datetime
import matplotlib.dates as mdates
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('sample_date.csv', index_col='date', parse_dates=True)

print(df)
print(type(df.index))

#
# 欠落した日付を補完して対応する測定値をゼロにする
#
df_new=df.asfreq('1D',fill_value=0)
print(df_new)

rs="resampled.csv"
df_new.to_csv(rs)

#
# 読み込み直して描画する
#
df = pd.read_csv(rs)

print(df)

#df.plot(x="date",kind='bar')
df.plot(x="date")

plt.xticks(rotation=90)
plt.grid(True, linestyle='dotted', linewidth=1)
plt.subplots_adjust(bottom=0.2, top=0.9)

plt.savefig("resampled.png")
ykishi@dezembro pandas-test % 
```
# 実行結果
```
ykishi@dezembro pandas-test % ./resample_test.py 
            apple  orange
date                     
2021-04-05    600     192
2021-04-06   5050     229
2021-04-09     50    2229
2021-04-11     50    4524
2021-04-15      0    8000
2021-04-21    450    5900
2021-04-25   4500   15900
2021-04-28      1      34
2021-04-29    300     155
2021-05-01   8999    6660
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
            apple  orange
date                     
2021-04-05    600     192
2021-04-06   5050     229
2021-04-07      0       0
2021-04-08      0       0
2021-04-09     50    2229
2021-04-10      0       0
2021-04-11     50    4524
2021-04-12      0       0
2021-04-13      0       0
2021-04-14      0       0
2021-04-15      0    8000
2021-04-16      0       0
2021-04-17      0       0
2021-04-18      0       0
2021-04-19      0       0
2021-04-20      0       0
2021-04-21    450    5900
2021-04-22      0       0
2021-04-23      0       0
2021-04-24      0       0
2021-04-25   4500   15900
2021-04-26      0       0
2021-04-27      0       0
2021-04-28      1      34
2021-04-29    300     155
2021-04-30      0       0
2021-05-01   8999    6660
          date  apple  orange
0   2021-04-05    600     192
1   2021-04-06   5050     229
2   2021-04-07      0       0
3   2021-04-08      0       0
4   2021-04-09     50    2229
5   2021-04-10      0       0
6   2021-04-11     50    4524
7   2021-04-12      0       0
8   2021-04-13      0       0
9   2021-04-14      0       0
10  2021-04-15      0    8000
11  2021-04-16      0       0
12  2021-04-17      0       0
13  2021-04-18      0       0
14  2021-04-19      0       0
15  2021-04-20      0       0
16  2021-04-21    450    5900
17  2021-04-22      0       0
18  2021-04-23      0       0
19  2021-04-24      0       0
20  2021-04-25   4500   15900
21  2021-04-26      0       0
22  2021-04-27      0       0
23  2021-04-28      1      34
24  2021-04-29    300     155
25  2021-04-30      0       0
26  2021-05-01   8999    6660
ykishi@dezembro pandas-test %
```
![resampled.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1182287/2d5e341b-9925-8dbd-518f-241790f859c8.png)