简体   繁体   English

最常用的值是使用pandas.DataFrame.resample

[英]Most frequent value using pandas.DataFrame.resample

I am using pandas.DataFrame.resample to resample a grouped Pandas dataframe with a timestamp index. 我正在使用pandas.DataFrame.resample来重新采样带有时间戳索引的分组Pandas dataframe

In one of the columns, I would like to resample such that I select the most frequent value. 在其中一列中,我想重新采样,以便选择最常用的值。 At the moment, I am only having success using NumPy functions like np.max or np.sum etc. 目前,我只是成功使用np.maxnp.sum等NumPy函数。

#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)

The previous code works because I have used NumPy functions. 之前的代码有效,因为我使用了NumPy函数。 However, if I want to use resample by most frequent value, I am not sure. 但是,如果我想以最常见的价值使用重新采样,我不确定。 I try defining a custom function like 我尝试定义一个自定义函数

def frequent(x):
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]

However, if I now do: 但是,如果我现在这样做:

how_dict = {0: np.max, 1: frequent}

I get an empty dataframe... 我得到一个空的数据帧......

df = test.groupby(group).resample('48 h',how=how_dict)
df.shape

Your resample period is too short, so when a group is empty on a period, your user function raise a ValueError not kindly caught by pandas . 您的重新采样周期太短,因此当一个组在一段时间内为空时,您的用户函数会引发一个不会被pandas捕获的ValueError。

But it works without empty groups, for example with regular groups: 但它没有空组,例如使用常规组:

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

Or with bigger periods : 或者更长的时期:

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

EDIT 编辑

A workaround can be to manage the empty case : 解决方法可以是管理空案例:

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

For 对于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM