-
Notifications
You must be signed in to change notification settings - Fork 0
/
RollingTimeWindowsBookStores.py
51 lines (35 loc) · 1.24 KB
/
RollingTimeWindowsBookStores.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import yaml
import mysql.connector
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
db = yaml.safe_load(open("db.yaml"))
config = {
'user': db['user'],
'password': db['pwrd'],
'host': db['host'],
'database': 'MRTS',
'auth_plugin': 'mysql_native_password'
}
book = "Book"
grocery = "Grocery"
store_name = book
cnx = mysql.connector.connect(**config)
store_sql= pd.read_sql_query(f"""
SELECT value, period
FROM mrts_table
WHERE Kind_of_Business = "{store_name} stores"
ORDER BY period;
""", cnx)
store = pd.DataFrame(store_sql, columns=["value", "period"])
plt.plot(store["period"], store["value"])
plt.gca().set(title=f"Monthly Sales of {store_name} Stores", xlabel = "Months", ylabel = "Sales")
ma4_store = store["value"].rolling(4).mean()
plt.figure()
plt.plot(store["period"], ma4_store, 'g')
plt.gca().set(title=f"Monthly Sales of {store_name} Stores with 4 month moving average", xlabel = "Months", ylabel = "Sales")
ma12_store = store["value"].rolling(12).mean()
plt.figure()
plt.plot(store["period"], ma12_store, 'k')
plt.gca().set(title=f"Monthly Sales of {store_name} Stores with 12 month moving average", xlabel = "Months", ylabel = "Sales")
plt.show()