Converting daily data to weekly means and medians
<p>I have a list of dicts like this:</p>

<pre><code>[
    {'2016-06-11': 10, 
     '2016-06-09': 10, 
     'ID': 1, 
     '2016-06-04': 10,
     '2016-06-07': 10,
     '2016-06-06': 10,
     '2016-06-01': 10,
     '2016-06-03': 10,
     'type': 'primary',
     '2016-06-05': 10,
     '2016-06-10': 10,
     '2016-06-02': 10,
     '2016-06-08': 10}, 
    {'2016-06-11': 2,
     '2016-06-09': 1,
     'ID': 2,
     'type': 'secondary',
     '2016-06-04': 1,
     '2016-06-07': 1,
     '2016-06-06': 1,
     '2016-06-01': 1,
     '2016-06-03': 1,
     '2016-06-05': 1,
     '2016-06-10': 2,
     '2016-06-02': 1,
     '2016-06-08': 1}
]
</code></pre>

<p>I need to convert this to a similar list of dicts, where the keys would be weeks (starting on Mondays, so eg <code>2016-06-03 - 2016-06-09</code>) or months (so eg <code>2016-06</code>), and the values would be either the mean or median of that week/month's values. What would be the simplest way to do this?</p>

<p>I think you can <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html" rel="nofollow"><code>resample</code></a> by <code>months</code>, aggregate <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.tseries.resample.Resampler.mean.html" rel="nofollow"><code>mean</code></a> or <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.tseries.resample.Resampler.median.html" rel="nofollow"><code>median</code></a> and last create <code>list</code> of <code>dict</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html" rel="nofollow"><code>DataFrame.to_dict</code></a>:</p>

<pre><code>df = pd.DataFrame(d)
print (df)
   2016-06-01  2016-06-02  2016-06-03  2016-06-04  2016-06-05  2016-06-06  \
0          10          10          10          10          10          10   
1           1           1           1           1           1           1   

   2016-06-07  2016-06-08  2016-06-09  2016-06-10  2016-06-11  ID       type  
0          10          10          10          10          10   1    primary  
1           1           1           1           2           2   2  secondary

df.set_index(['type', 'ID'], inplace=True)
df.columns = pd.to_datetime(df.columns)
df = df.T.resample('M').mean()
df.index = df.index.strftime('%Y-%m')
print (df)
type    primary secondary
ID            1         2
2016-06    10.0  1.181818

print (df.T.reset_index().to_dict(orient='records'))
[{'type': 'primary', '2016-06': 10.0, 'ID': 1}, 
 {'type': 'secondary', '2016-06': 1.1818181818181819, 'ID': 2}]
</code></pre>

<hr>

<pre><code>df.set_index(['type', 'ID'], inplace=True)
df.columns = pd.to_datetime(df.columns)
df = df.T.resample('M').median()
df.index = df.index.strftime('%Y-%m')
print (df)
type    primary secondary
ID            1         2
2016-06      10         1

print (df.T.reset_index().to_dict(orient='records'))
[{'type': 'primary', '2016-06': 10, 'ID': 1}, 
 {'type': 'secondary', '2016-06': 1, 'ID': 2}]
</code></pre>

<p>Another solution instead <code>reample</code> is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a> by month period created by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.to_period.html" rel="nofollow"><code>DatetimeIndex.to_period</code></a>:</p>

<pre><code>df = df.groupby([df.index.to_period('m')]).mean()
df = df.groupby([df.index.to_period('m')]).median()
</code></pre>

python date pandas mean median