title: Pandas: combine resampling and group by. How do I average values within same groups before summing them up in a time bin?
body: <p>I have tabular data (described by col1, col2,...) associated to a Timestamp and a custom made Group. Example:</p>
  
  <pre><code>           Group  Col1  Col2  Col3
  Time
  2020-05-18     A    10    20    30
  2020-05-18     B    10    20    30
  2020-05-18     C    10    20    30
  2020-05-19     A    10    20    30
  2020-05-19     B    10    20    30
  2020-05-19     C    10    20    30
  2020-05-20     A    10    20    30
  2020-05-20     B    10    20    30
  2020-05-20     C    10    20    30
  2020-05-21     A    10    20    30
  2020-05-21     B    10    20    30
  2020-05-21     C    10    20    30
  </code></pre>
  
  <p>This dataframe can be generated by:</p>
  
  <pre class="lang-py prettyprint-override"><code>dataframe = pd.DataFrame({"Time": ["2020-05-18", "2020-05-18", "2020-05-18", "2020-05-19", "2020-05-19", "2020-05-19", "2020-05-20", "2020-05-20", "2020-05-20", "2020-05-21", "2020-05-21", "2020-05-21"], "Group": ["A","B","C","A","B","C","A","B","C","A","B","C"], "Col1": 12*[10], "Col2": 12*[20], "Col3": 12*[30]})
  dataframe["Time"] = pd.to_datetime(dataframe["Time"])
  dataframe = dataframe.set_index("Time")
  </code></pre>
  
  <p>I would like to create time bins (like with resample function), average values WITHIN the same group, and  THEN sum them up within the same time bin.
  But if I write <code>dataframe.resample("2D").sum()</code>, values will be direclty summed up over all the groups without first being averaged in the same group. Like this:</p>
  
  <pre><code>            Col1  Col2  Col3
  Time
  2020-05-18    60   120   180
  2020-05-20    60   120   180
  </code></pre>
  
  <p>But what I need is to average values belonging to the same group BEFORE summing them up. In this case the desired output would be:</p>
  
  <pre><code>            Col1  Col2  Col3
  Time
  2020-05-18    30   60   90
  2020-05-20    30   60   90
  </code></pre>
  
  <p>I tried to apply groupby after resampling (e.g. <code>dataframe.resample("2D").grouby("Group")</code>), but it raises a TypeError ("'TimeGrouper' object is not callable")</p>
  
  <p>What could be a way to solve this problem? Thanks in advance.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Output of <code>dataframe.groupby(["Time","Group"]).mean()</code> :</p>
  
  <pre><code>                  Col1  Col2  Col3
  Time       Group
  2020-05-18 A        10    20    30
             B        10    20    30
             C        10    20    30
  2020-05-19 A        10    20    30
             B        10    20    30
             C        10    20    30
  2020-05-20 A        10    20    30
             B        10    20    30
             C        10    20    30
  2020-05-21 A        10    20    30
             B        10    20    30
             C        10    20    30
  </code></pre>