-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforecast_example.py
63 lines (51 loc) · 2.24 KB
/
forecast_example.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
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 8 16:56:52 2018
@author: Aveedibya Dey
"""
import pandas as pd
import numpy as np
import datetime
def fcst_wklyavg(df, fcst_range=90, n_week=6, stop_at_futuredates=0, data_period=7):
#df = pd.read_csv('C:\\Users\\agarw\\Documents\\transaction_data.csv').dropna()
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')
#fcst_range = 90
fcst_start = max(df['Date']) + datetime.timedelta(days=1)
fcst_end = max(df['Date']) + datetime.timedelta(days=fcst_range)
df2 = pd.DataFrame({'Date': pd.date_range(fcst_start, fcst_end, freq='D')})
df2['Volume'] = None
df_final = pd.concat([df, df2]).reset_index().drop('index', axis=1)
df_final['Forecast'] = 0.0
print(df_final.dtypes)
df_final = df_final.convert_objects(convert_numeric=True)
if stop_at_futuredates == 0:
#n-week average:
n = n_week
for i in (range(df.shape[0], df_final.shape[0])):
curr_count = 0.0
curr_sum = 0.0
for j in range(1, int(n+1)):
#print("i is:",i)
#print("j is:",j)
if np.isnan(df_final['Volume'][i-j*data_period]):
if np.isnan(df_final['Forecast'][i-j*data_period]) == False:
curr_sum += df_final['Forecast'][i-j*data_period]
curr_count += 1
#print("Forecast value taken:",df_final['Forecast'][i-j*7])
else:
curr_sum += df_final['Volume'][i-j*data_period]
curr_count += 1
#print("Volume value taken:",df_final['Volume'][i-j*7])
df_final['Forecast'][i] = curr_sum/curr_count
#print(curr_sum, curr_count)
#Add year to dataframe
df_final['Year'] = df_final['Date'].dt.year
#Add month to dataframe
df_final['Month'] = df_final['Date'].dt.month
#Add weekday name in the dataframe
df_final['WkdyNm'] = df_final['Date'].dt.weekday_name
print('------------------Moving Average Forecast Generated-------------------')
return df_final
if __name__ == "__main__":
df_final = fcst_wklyavg(df)
print(df_final.tail())