使用 pandas.DataFrame.resample 最频繁的值

Fra*_*sco 5 python numpy pandas

我正在使用时间戳索引pandas.DataFrame.resample重新采样分组的 Pandas dataframe

在其中一列中,我想重新采样,以便选择最常见的值。目前,我只成功使用 NumPy 函数,如np.maxnp.sum等。

#generate test dataframe
data = np.random.randint(0,10,(366,2))
index = pd.date_range(start=pd.Timestamp('1-Dec-2012'), periods=366, unit='D')
test = pd.DataFrame(data, index=index)

#generate group array
group =  np.random.randint(0,2,(366,))

#define how dictionary for resample
how_dict = {0: np.max, 1: np.min}

#perform grouping and resample
test.groupby(group).resample('48 h',how=how_dict)
Run Code Online (Sandbox Code Playgroud)

前面的代码有效,因为我使用了 NumPy 函数。但是,如果我想按最频繁的值使用重采样,我不确定。我尝试定义一个自定义函数,如

def frequent(x):
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]
Run Code Online (Sandbox Code Playgroud)

但是,如果我现在这样做:

how_dict = {0: np.max, 1: frequent}
Run Code Online (Sandbox Code Playgroud)

我得到一个空的数据框...

df = test.groupby(group).resample('48 h',how=how_dict)
df.shape
Run Code Online (Sandbox Code Playgroud)

B. *_* M. 5

您的重采样时间太短,因此当某个时间段的组为空时,您的用户函数会引发一个 ValueError 不会被 pandas 捕获。

但它在没有空组的情况下工作,例如使用常规组:

In [8]: test.groupby(arange(366)%2).resample('48h',how=how_dict).head()
Out[8]: 
              0  1
0 2012-12-01  4  8
  2012-12-03  0  3
  2012-12-05  9  5
  2012-12-07  3  4
  2012-12-09  7  3
Run Code Online (Sandbox Code Playgroud)

或者更大的时期:

In [9]: test.groupby(group).resample('122D',how=how_dict)
Out[9]: 
              0  1
0 2012-12-02  9  0
  2013-04-03  9  0
  2013-08-03  9  6
1 2012-12-01  9  3
  2013-04-02  9  7
  2013-08-02  9  1
Run Code Online (Sandbox Code Playgroud)

编辑

解决方法可以是管理空案例:

def frequent(x):
    if len(x)==0 : return -1
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]
Run Code Online (Sandbox Code Playgroud)

为了

In [11]: test.groupby(group).resample('48h',how=how_dict).head()
Out[11]: 
               0  1
0 2012-12-01   5  3
  2012-12-03   3  4
  2012-12-05 NaN -1
  2012-12-07   5  0
  2012-12-09   1  4
Run Code Online (Sandbox Code Playgroud)