Window Functions
Window Functions¶
Window functions let us apply something over segments of our data. To begin with, let’s get a set of data that is supposed to be daily sales. Don’t worry about the code below it is just creating the data.
np.random.seed(3)
daily_sales = pd.Series(np.random.normal(100, 2000, 100))
daily_sales = daily_sales.clip(0, None) + np.array(range(100)) * 80
daily_sales.index = daily_sales.index + 1
daily_sales.plot(kind='line')
plt.xlabel("Day")
plt.ylabel("Sales")
plt.title("Sales by Day")
plt.show()
Expanding Window¶
An expanding window is one where we go through each row of data and run a function using that row and everything before it. For our data here there is an obvious application, we can find the running total sales over the days. The way we call it is calling expanding() on a series or dataframe and then calling another function after it. The code below creates total sales by running expanding and adding up the total sales at each point.
#Get ther running total with an expanding window
total_sales = daily_sales.expanding().sum()
print(total_sales)
We may also want to have the first value be equal to 0. We can do that by using loc and setting the value to 0.
#Set t=0 to 0
total_sales.loc[0] = 0
print(total_sales)
It is out of order, though, and so we want to use sort_index() to sort our data into the correct order.
#Sort total sales
total_sales = total_sales.sort_index()
print(total_sales)
#Plot total sales
total_sales.plot(kind='line')
plt.xlabel("Day")
plt.ylabel("Sales")
plt.title("Total Sales")
plt.show()
Rolling Window¶
A rolling window takes the last n rows including the current one and computes a calculation. For this example, we might be interested in smoothing out our daily sales to see if there is a trend. To do this we can use rolling and the function mean to find the rolling mean. You will notice larger numbers for n leads to smoother functions. For example:
#Find the averages of the last 3 days
avg3d = daily_sales.rolling(3).mean()
#Plot with the regular sales
daily_sales.plot(kind='line')
avg3d.plot(kind='line')
plt.xlabel("Day")
plt.ylabel("Sales")
plt.title("Sales by Day")
plt.legend(['Daily Sales', '3 Day Average'])
plt.show()
#Find the averages of the last 10 days
avg10d = daily_sales.rolling(10).mean()
#Plot with the regular sales
daily_sales.plot(kind='line')
avg10d.plot(kind='line')
plt.xlabel("Day")
plt.ylabel("Sales")
plt.title("Sales by Day")
plt.legend(['Daily Sales', '10 Day Average'])
plt.show()