Group by year/month/day in pandas
<p>Assume having the following <code>DataFrame</code>:</p>

<pre><code>rng = pd.date_range('1/1/2011', periods=72, freq='H')
np.random.seed(10)
n = 10
df = pd.DataFrame(
    {
        "datetime": np.random.choice(rng,n),
        "cat": np.random.choice(['a','b','b'], n),
        "val": np.random.randint(0,5, size=n)
        }
    )
</code></pre>

<p>If I now <code>groupby</code>:</p>

<pre><code>gb = df.groupby(['cat','datetime']).sum()
</code></pre>

<p>I get the totals for each <code>cat</code> for each hour:</p>

<pre><code>cat datetime            val
a   2011-01-01 00:00:00 1
    2011-01-01 09:00:00 3
    2011-01-02 16:00:00 1
    2011-01-03 16:00:00 1
b   2011-01-01 08:00:00 4
    2011-01-01 15:00:00 3
    2011-01-01 16:00:00 3
    2011-01-02 04:00:00 4
    2011-01-02 05:00:00 1
    2011-01-02 12:00:00 4
</code></pre>

<p>However, I would like to have something like:</p>

<pre><code>cat datetime   val
a   2011-01-01 4
    2011-01-02 1
    2011-01-03 1
b   2011-01-01 10
    2011-01-02 9
</code></pre>

<p>I could get the desired result by adding another column called <code>date</code>:</p>

<pre><code>df['date'] = df.datetime.apply(pd.datetime.date)
</code></pre>

<p>and then do a similar <code>groupby</code>: <code>df.groupby(['cat','date']).sum()</code>. But I am interested whether there's more pythonic way to do it? In addition, I might want to have a look on the month or year level. So, what would be the right way?</p>