あなたはdate_range
で使用isin
を試すことができます。
#converting column date to datetime
df['date'] = pd.to_datetime(df.date)
print df
date sales
0 2014-03-13 10000.000
1 2014-03-21 2000.000
2 2014-03-27 2000.000
3 2014-03-17 200.000
4 2014-03-17 5.000
5 2014-03-28 70.000
6 2014-03-29 200.000
7 2014-03-29 5.000
8 2014-03-30 25.000
9 2014-03-31 0.020
10 2014-03-31 12.000
11 2014-03-31 0.022
print df[df['date'].isin(pd.date_range(end=df.date.max(), periods=3))]
date sales
6 2014-03-29 200.000
7 2014-03-29 5.000
8 2014-03-30 25.000
9 2014-03-31 0.020
10 2014-03-31 12.000
11 2014-03-31 0.022
一部dates
が欠落している場合は、それが複雑である - merge
でresample
を使用して、fillna
:
print df
date sales
0 2014-03-13 10000.000
1 2014-03-21 2000.000
2 2014-03-27 2000.000
3 2014-03-17 200.000
4 2014-03-17 5.000
5 2014-03-17 70.000
6 2014-03-21 200.000
7 2014-03-27 5.000
8 2014-03-27 25.000
9 2014-03-31 0.020
10 2014-03-31 12.000
11 2014-03-31 0.022
#resample by days and merge to original df, fill NaN to 0 in column sales
df1 = df.set_index('date').resample('D').first().reset_index().drop('sales', axis=1)
df2 = pd.merge(df1, df, on='date', how='left').fillna({'sales': 0})
print df2[df2['date'].isin(pd.date_range(end=df.date.max(), periods=3))]
date sales
21 2014-03-29 0.000
22 2014-03-30 0.000
23 2014-03-31 0.020
24 2014-03-31 12.000
25 2014-03-31 0.022
最大の日付が2014-03-31の場合は、31,30,29の日付のデータが必要です –
はい、サンプルは30と29の日付ではありません。 – jezrael
最大の日付から最後の10のデータが必要な場合 手段? –