【发布时间】:2016-02-10 08:59:05
【问题描述】:
我有以下例子:
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 的东西?
这是由 timeseries scikit 提供的:
scikits.timeseries.TimeSeries.fill_missing_dates
【问题讨论】:
-
也许可以帮助
print df_concat.resample('10T') -
df.fill_missing_dates(fill_value=...)等价于df.fillna(value=...)(如果 NaN 已经存在并且您不更改频率),或df.resample(freq).fillna(value)
标签: python pandas time-series missing-data