resample dataframe with python
<p>I need to resample a dataframe using pandas.DataFrame.resample function like this : </p>

<pre><code>data.set_index('TIMESTAMP').resample('24min', how='sum')
</code></pre>

<p>This works without no problem, but when I try call the fucntion with 'xmin" where x is a general argment</p>

<pre><code>  data.set_index('TIMESTAMP').resample('xmin', how='sum')
</code></pre>

<p>It cannot works</p>

<p>Any idea please?</p>

<p>Thank you</p>

<p><strong>EDIT</strong></p>

<pre><code>def ratio_activ_equip(data, date_deb, date_fin, step):      

 # filtre_site(data, site)
filtre_date(data, date_deb, date_fin)
xmin = 'stepmin'
data.set_index('TIMESTAMP').resample(xmin, how='sum')
res = data.iloc[:,1:10] = data.iloc[:,1:10].divide(data.sum(axis=1), axis=0)
res = data
return res
</code></pre>

<p><strong>EDIT2</strong></p>

<pre><code>def ratio_activ_equip(data, date_deb, date_fin, step):       #
     # filtre_site(data, site)
    filtre_date(data, date_deb, date_fin)
    #step = 10
    xmin = str(step) + 'min'
    data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
    data.set_index('TIMESTAMP').resample(xmin, how='sum')
    res = data.iloc[:,1:10] = data.iloc[:,1:10].divide(data.sum(axis=1), axis=0)
    res = data
    return res
</code></pre>

<p>When I call this fucntion like this : </p>

<pre><code>res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30)
</code></pre>

<p>I get this error : </p>

<blockquote>
<pre><code>KeyError: 'TIMESTAMP'
</code></pre>
</blockquote>

<p>Any idea please?</p>

<p>IIUC you need pass variable <code>xmin</code>:</p>

<pre><code>xmin = '24min'
data.set_index('TIMESTAMP').resample(xmin, how='sum')
</code></pre>

<p>More general is convert <code>int</code> value to <code>str</code> and add substring <code>min</code>:</p>

<pre><code>step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')
</code></pre>

<hr>

<pre><code>step = 10
data = data.set_index('TIMESTAMP').resample(str(step) + 'min', how='sum')
</code></pre>

python pandas dataframe resampling datetimeindex