2016-02-10 115 views
2

我下面舉個例子:快捷填充缺少的日期

import numpy as np 
import pandas as pd 

idx1 = pd.period_range('2015-01-01', freq='10T', periods=1000) 

idx2 = pd.period_range('2016-01-01', freq='10T', periods=1000) 

df1 = pd.DataFrame(np.random.randn(1000), index=idx1, 
        columns=['A']) 
df2 = pd.DataFrame(np.random.randn(1000), index=idx2, 
        columns=['A']) 

frames = [df1, df2] 

df_concat = pd.concat(frames) 

現在,我想知道在df_concat

所以我填寫的日期和重建索引數據幀丟失的日期數:

start_total = df1.index[0] 
end_total = df2.index[-1] 
idx_total = pd.period_range(start=start_total, end=end_total, freq='10T') 
df_total = df_concat.reindex(idx_total, fill_value=np.nan) 
df_miss = df_total[df_total.isnull()] 

是否有最後一個代碼段的較短版本?

類似df_concat.fill_missing_dates? 這是提供時間序列scikit: scikits.timeseries.TimeSeries.fill_missing_dates

+0

也許幫助'打印df_concat.resample( '10T')'' – jezrael

+1

df.fill_missing_dates(fill_value =。 ..)'相當於'df.fillna(value = ...)'(如果NaN已經在那裏,並且你沒有改變頻率)或者'df.resample(freq).fillna(value)' – joris

回答

1

我認爲你可以使用resample

df_total = df_concat.resample('10T') 
print df_total[df_total.isnull()] 

        A 
2015-01-01 00:00:00 NaN 
2015-01-01 00:10:00 NaN 
2015-01-01 00:20:00 NaN 
2015-01-01 00:30:00 NaN 
2015-01-01 00:40:00 NaN 
2015-01-01 00:50:00 NaN 
2015-01-01 01:00:00 NaN 
2015-01-01 01:10:00 NaN 
2015-01-01 01:20:00 NaN 
2015-01-01 01:30:00 NaN 
2015-01-01 01:40:00 NaN 
2015-01-01 01:50:00 NaN 
2015-01-01 02:00:00 NaN 
2015-01-01 02:10:00 NaN 
2015-01-01 02:20:00 NaN