-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
75 lines (63 loc) · 3.02 KB
/
functions.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
64
65
66
67
68
69
70
71
72
73
74
75
import datetime as dt
from re import sub
import pandas as pd
def update_report(path_to_date_verification, path_to_estimated_completion, transit_days, include_weekends):
## Make a dataframe of the requested lines to update
promise_date = pd.DataFrame(pd.read_excel(
path_to_date_verification,
header=1,
sheet_name='Requested Updates'
))
# Create an index column using order-release
promise_date['Order-Line'] = promise_date['PO #'].astype(str) + '-' + promise_date['Line #'].astype(str)
promise_date = promise_date.set_index(['Order-Line'])
## Make a dataframe of the input data
estimated_date = pd.DataFrame(pd.read_excel(
path_to_estimated_completion,
header=0,
sheet_name=0,
usecols='A:H'
))
# Create an index column using order-release
estimated_date['Order-Line'] = estimated_date['PO'].astype(str) + '-' + estimated_date['Line'].astype(str)
estimated_date = estimated_date.set_index(['Order-Line'])
## Find and fix Excel-formatted dates
if estimated_date['Estimated Ship Date'].dtype == 'object':
estimated_date = estimated_date.sort_index()
for i, row in estimated_date[estimated_date['Estimated Ship Date'].astype(str).str.match('\d{5}')].iterrows():
excel_date = row['Estimated Ship Date']
try:
fixed_date = pd.to_datetime(excel_date, unit='d', origin='1899-12-30')
estimated_date.loc[i, 'Estimated Ship Date'] = fixed_date
except:
print('Dates cannot be automatically converted. Rows with malformed dates will be ignored.')
## Remove any malformed dates
estimated_date['Estimated Ship Date'] = pd.to_datetime(estimated_date['Estimated Ship Date'], errors='coerce')
estimated_date.dropna(inplace=True, subset=['Estimated Ship Date'])
## Look up estimated completion dates from scheduling sheet
promise_date = promise_date.join(
estimated_date['Estimated Ship Date'],
on=['Order-Line'])
# Update promise dates
if include_weekends:
promise_date['Verified Current Promise Date'] = promise_date['Estimated Ship Date'] + pd.tseries.offsets.DateOffset(transit_days)
else:
promise_date['Verified Current Promise Date'] = promise_date['Estimated Ship Date'] + pd.tseries.offsets.BusinessDay(transit_days)
promise_date.drop_duplicates(['PO #', 'Line #'], keep="first", inplace=True)
promise_date['Verified Current Promise Date'] = promise_date['Verified Current Promise Date'].dt.date
## Write promise dates into the Promise Date Verification template
with pd.ExcelWriter(
path_to_date_verification,
mode="a",
if_sheet_exists="overlay",
date_format="YYYY-MM-DD",
datetime_format="YYYY-MM-DD"
) as writer:
promise_date['Verified Current Promise Date'].to_excel(
writer,
sheet_name="Requested Updates",
startrow=2,
startcol=7,
header=False,
index=False
)