Most frequent value using pandas.DataFrame.resample
<p>I am using <code>pandas.DataFrame.resample</code> to resample a grouped Pandas <code>dataframe</code> with a timestamp index.</p>

<p>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 <code>np.max</code> or <code>np.sum</code> etc.</p>

<pre><code>#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)
</code></pre>

<p>The previous code works because I have used NumPy functions. However, if I want to use resample by most frequent value, I am not sure. I try defining a custom function like </p>

<pre><code>def frequent(x):
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]
</code></pre>

<p>However, if I now do:</p>

<pre><code>how_dict = {0: np.max, 1: frequent}
</code></pre>

<p>I get an empty dataframe...</p>

<pre><code>df = test.groupby(group).resample('48 h',how=how_dict)
df.shape
</code></pre>