Pandas group_by date and resample
<p>I have some data frame that looks like this:</p>

<pre><code>    A   B   C   date
0   J   Y   2   2013-02-01 14:21:02.070030
1   X   X   0   2013-02-01 15:49:33.110849
2   Y   D   9   2013-02-01 06:47:19.369514
3   Y   C   17  2013-02-01 08:56:11.751781
4   3   J   21  2013-02-01 14:19:12.017232
</code></pre>

<p>I'd like to group by date and then count, but omit the information about the hours, minutes, seconds, etc.</p>

<p>It seems like something like this works:</p>

<pre><code>df.set_index('date').resample('D').count()
</code></pre>

<p>Two questions:</p>

<ol>
<li>Why does that work? Is that the right way?</li>
<li>Why doesn't something like <code>df.group_by('date').resample('D').count()</code> work?</li>
</ol>

<p><code>resample</code> is in some sense just a special case of groupby - rather than grouping on distinct values, which is what <code>grouppy('date')</code> would do, it groups a time-based transformation of the index, which is why you need to set the index.  Alternatively, you could do:</p>

<pre><code>df.groupby(pd.Grouper(key='date', freq='D')).count()
</code></pre>

<p>In the upcoming version <code>0.19.0</code> you'll be able to write the above like this.</p>

<pre><code>df.resample('D', on='date').count()
</code></pre>

python pandas