Reputation: 4498
I have a dataframe edf, where I transform DATESENT into my datetimeindex and then I want to pivot it by week.
edf = pd.read_csv('C:\Users\j~\raw.csv', parse_dates=[6])
edf2 = edf[['DATESENT','Sales','Traffic]].copy()
edf2['DATESENT']=pd.to_datetime(edf2['DATESENT'],format='%m/%d/%Y')
edf2 = edf2.set_index(pd.DatetimeIndex(edf2['DATESENT']))
edf2.resample('w').sum()
edf2
SALES
DATESENT
2014-01-05 476
2014-01-12 67876
If understand correctly resample('w') leads to a time period from 2014-01-06 (Monday) until 2014-01-12(Sunday)?
Is there a way to change it so it would go from 2014-01-05(Sunday) to 2014-01-11(Saturday)? (in other words fiscal weeks)?
Also, right now the sum(), sums all columns up. Is there a way to sum sales and average traffic with the resample function?
Thank you
Upvotes: 0
Views: 1447
Reputation: 153460
df = pd.DataFrame({'Date':pd.date_range(start='2014-01-01', end='2014-01-31', step=1),'sales':np.random.randint(31),'traffic':np.random.randint(31)})
df = df.set_index('Date')
#Change the week start day to Monday.
df.resample('W-MON').agg(['sum'])
or
#Change the week start day to Sunday
df.resample('W-SUN').agg(['sum'])
Upvotes: 1