填写缺失日期的快捷方式

时间:2016-02-10 08:59:06

标签: python pandas time-series missing-data

我有以下示例:

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

1 个答案:

答案 0 :(得分: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