在按日期时间索引的 pandas 数据框中最常出现

Most frequent occurence in a pandas dataframe indexed by datetime

我有一个很大的 DataFrame,它由 datetime 索引,特别是按天索引。我正在寻找一个有效的函数,它针对每一列检查每周最常见的非空值,并输出一个数据框,该数据框按周索引,由这些周内最常见的值组成。

这是一个例子。以下 DataFrame 包含两周的每日数据:

                        0      1    
2015-11-12 00:00:00     8     nan   
2015-11-13 00:00:00     7     nan   
2015-11-14 00:00:00     nan   5   
2015-11-15 00:00:00     7     nan   
2015-11-16 00:00:00     8     nan   
2015-11-17 00:00:00     7     nan   
2015-11-18 00:00:00     5     nan   
2015-11-19 00:00:00     9     nan   
2015-11-20 00:00:00     8     nan   
2015-11-21 00:00:00     6     nan   
2015-11-22 00:00:00     6     nan   
2015-11-23 00:00:00     6     nan   
2015-11-24 00:00:00     6     nan   
2015-11-25 00:00:00     2     nan   

应转化为:

                        0    1    
2015-11-12 00:00:00     7    5
2015-11-19 00:00:00     6    nan

我的DataFrame很大所以效率很重要。谢谢。

编辑:如果可能的话,如果条目是元组(而不是我的示例中的浮点数),有人可以建议一种适用的方法吗?

您可以使用 resample 按周间隔对数据进行分组。然后,通过 pd.value_counts 和 select 计算出现的次数,最常见的 idxmax:

df.resample("7D").apply(lambda x: x.apply(pd.value_counts).idxmax())

                     0      1
2015-11-12 00:00:00  7.0    5.0
2015-11-19 00:00:00  6.0    NaN

编辑

这是另一个比上述解决方案更快的 numpy 版本:

def numpy_mode(series):
    values = series.values
    dropped = values[~np.isnan(values)]

    # check for empty array and return NaN
    if not dropped.size:
        return np.NaN

    uniques, counts = np.unique(series.dropna(), return_counts=True)
    return uniques[np.argmax(counts)]

df2.resample("7D").apply(lambda x: x.apply(get_mode))

                     0      1
2015-11-12 00:00:00  7.0    5.0
2015-11-19 00:00:00  6.0    NaN

这里是基于虚拟数据的计时(如需进一步改进,请查看 here):

%%timeit
df2.resample("7D").apply(lambda x: x.apply(pd.value_counts).idxmax())
>>> 100 loops, best of 3: 18.6 ms per loop

%%timeit 
df2.resample("7D").apply(lambda x: x.apply(get_mode))
>>> 100 loops, best of 3: 3.72 ms per loop

我也试过 scipy.stats.mode 但是它也比 numpy 解决方案慢:

size = 1000
index = pd.DatetimeIndex(start="2012-12-12", periods=size, freq="D")
dummy = pd.DataFrame(np.random.randint(0, 20, size=(size, 50)), index=index)
print(dummy.head)

            0   1   2   3   4   5   6   7   8   9   ...     40  41  42  43  44  45  46  47  48  49
2012-12-12  18  2   7   1   7   9   16  2   19  19  ...     10  2   18  16  15  10  7   19  9   6
2012-12-13  7   4   11  19  17  10  18  0   10  7   ...     19  11  5   5   11  4   0   16  12  19
2012-12-14  14  0   14  5   1   11  2   19  5   9   ...     2   9   4   2   9   5   19  2   16  2
2012-12-15  12  2   7   2   12  12  11  11  19  5   ...     16  0   4   9   13  5   10  2   14  4
2012-12-16  8   15  2   18  3   16  15  0   14  14  ...     18  2   6   13  19  10  3   16  11  4 

%%timeit
dummy.resample("7D").apply(lambda x: x.apply(get_mode))
>>> 1 loop, best of 3: 926 ms per loop

%%timeit
dummy.resample("7D").apply(lambda x: x.apply(pd.value_counts).idxmax())
>>> 1 loop, best of 3: 5.84 s per loop

%%timeit
dummy.resample("7D").apply(lambda x: stats.mode(x).mode)
>>> 1 loop, best of 3: 1.32 s per loop