<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Notebook accompanying pbpython article - Pandas Grouper and Agg Functions Explained
Step1:  Read in the sample sales file then convert the date column to a proper date time column
Step2:  Example showing how resample can be used along with set_index
Step3:  A more complex example with a groupby
Step4:  A simpler example using pd.Grouper
Step5:  Some more examples using various off set alisases - http
Step6:  Now show how to use the new .agg function
First, how to get summary stats without agg
Step7:  Using .agg for sums and means across multiple columns
Step8:  Passing a dictionary containing different operations per column
Step9:  Using custom functions
Step10:  Clean up the naming in the output by defining the name for get_max
Step11:  Using an OrderedDictionary to maintain column order
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import collections
Explanation: Notebook accompanying pbpython article - Pandas Grouper and Agg Functions Explained
End of explanation
df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=True")
df["date"] = pd.to_datetime(df['date'])
df.head()
df.dtypes
Explanation: Read in the sample sales file then convert the date column to a proper date time column
End of explanation
df.set_index('date').resample('M')["ext price"].sum()
Explanation: Example showing how resample can be used along with set_index
End of explanation
df.set_index('date').groupby('name')["ext price"].resample("M").sum().head(20)
Explanation: A more complex example with a groupby
End of explanation
df.groupby(['name', pd.Grouper(key='date', freq='M')])['ext price'].sum().head(20)
df.groupby(['name', pd.Grouper(key='date', freq='A-DEC')])['ext price'].sum()
# This works but is kind of slow and probably not that useful for this data set
#df.groupby(['name', pd.Grouper(key='date', freq='60s')])['ext price'].sum()
Explanation: A simpler example using pd.Grouper
End of explanation
df.groupby(['name', pd.Grouper(key='date', freq='W-MON')])['ext price'].sum()
df.groupby(['name', 'sku', pd.Grouper(key='date', freq='A-DEC')])['ext price'].sum()
Explanation: Some more examples using various off set alisases - http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
End of explanation
df[["ext price", "quantity"]].sum()
df[["ext price", "quantity"]].mean()
Explanation: Now show how to use the new .agg function
First, how to get summary stats without agg
End of explanation
df[["ext price", "quantity", "unit price"]].agg(['sum', 'mean'])
Explanation: Using .agg for sums and means across multiple columns
End of explanation
df.agg({'ext price': ['sum', 'mean'], 'quantity': ['sum', 'mean'], 'unit price': ['mean']})
Explanation: Passing a dictionary containing different operations per column
End of explanation
get_max = lambda x: x.value_counts(dropna=False).index[0]
df.agg({'ext price': ['sum', 'mean'], 'quantity': ['sum', 'mean'], 'unit price': ['mean'], 'sku': [get_max]})
Explanation: Using custom functions
End of explanation
get_max.__name__ = "most frequent"
df.agg({'ext price': ['sum', 'mean'], 'quantity': ['sum', 'mean'], 'unit price': ['mean'], 'sku': [get_max]})
Explanation: Clean up the naming in the output by defining the name for get_max
End of explanation
f = collections.OrderedDict([('ext price', ['sum', 'mean']), ('quantity', ['sum', 'mean']), ('sku', [get_max])])
df.agg(f)
Explanation: Using an OrderedDictionary to maintain column order
End of explanation
<END_TASK>