Pandas:按时间处理数据帧索引中的缺失数据

Pandas: Working with missing data in data frame index by time

我得到了以下 DataFrame(示例):

               date  Value1 Value2
2007-05-25 11:50:00   1       15 
2007-05-25 12:00:00   2       30
2007-05-25 12:10:00   3       25
2007-05-25 12:50:00   2       34
2007-05-25 13:00:00   9       35
2007-05-25 13:10:00   6       10

您可以看到从 2007-05-25 12:20:002007-05-25 12:40:00 的数据丢失了。我想做的是这样的数据框:

               date  Value1 Value2
2007-05-25 11:50:00   1       15 
2007-05-25 12:00:00   2       30
2007-05-25 12:10:00   3       25
2007-05-25 12:20:00   NaN     NaN
2007-05-25 12:30:00   NaN     NaN
2007-05-25 12:40:00   NaN     NaN
2007-05-25 12:50:00   2       34
2007-05-25 13:00:00   9       35
2007-05-25 13:10:00   6       10

一般我想看看我是哪个时间没有数据。我在想这样的事情:

DataRange = pd.date_range(data.index[0],data.index[-1])
data = data.reindex(DataRange, fill_value=nan)

但据我所知,通过测试 date_range 函数的名称表明了它的日期范围。所以我想知道是否有类似的分钟(在我的例子中是 10 分钟)或者是否有另一种方法来实现它。

调用 resample 并将规则作为“10Min”传递:

In [309]:

df.resample('10Min')
Out[309]:
                     Value1  Value2
date                               
2007-05-25 11:50:00       1      15
2007-05-25 12:00:00       2      30
2007-05-25 12:10:00       3      25
2007-05-25 12:20:00     NaN     NaN
2007-05-25 12:30:00     NaN     NaN
2007-05-25 12:40:00     NaN     NaN
2007-05-25 12:50:00       2      34
2007-05-25 13:00:00       9      35
2007-05-25 13:10:00       6      10

可以找到一些例子here