Cyrine Ezzahra Cyrine Ezzahra - 2 months ago 19
Python Question

resample dataframe with python

I need to resample a dataframe using pandas.DataFrame.resample function like this :

data.set_index('TIMESTAMP').resample('24min', how='sum')


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

data.set_index('TIMESTAMP').resample('xmin', how='sum')


It cannot works

Any idea please?

Thank you

EDIT

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


EDIT2

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


When I call this fucntion like this :

res = ratio_activ_equip(y, '2016-05-10 22:00:00', '2016-05-14 22:00:00', 30)


I get this error :


KeyError: 'TIMESTAMP'



Any idea please?

Answer

IIUC you need pass variable xmin:

xmin = '24min'
data.set_index('TIMESTAMP').resample(xmin, how='sum')

More general is convert int value to str and add substring min:

step = 10
xmin = str(step) + 'min'
data = data.set_index('TIMESTAMP').resample(xmin, how='sum')

step = 10
data = data.set_index('TIMESTAMP').resample(str(step) + 'min', how='sum')
Comments