I have a large pandas
dataframe df
with warehouse data showing amounts of received items.
Imagine the relevant part of the structure as:
Date SKU received
2017-05-29 sku1 0
2017-05-30 sku1 0
2017-05-31 sku1 0
2017-06-01 sku1 0
2017-06-02 sku1 6
2017-06-03 sku1 2
2017-05-29 sku2 4
2017-05-30 sku2 4
2017-05-31 sku2 0
2017-06-01 sku2 0
2017-06-02 sku2 0
2017-06-03 sku2 24
From here I would like to reconstruct the order process. I know, that the stock level is reviewed on mondays, based on the stock level, new orders are placed. Orders arrive at the warehouse roughly a week later, sometimes split into multiple shippings.
I thought of creating an extra column for the weekdays (df["Weekday"]
) and for the placed orders (df["Order"]
). Based on the weekday, I would like to aggregate the data of the "received" column for the next 4 to 11 days, restricted to the relevant SKU.
The output could look like this:
Date SKU received Weekday Order
2017-05-29 sku1 0 0 8
2017-05-30 sku1 0 1 0
2017-05-31 sku1 0 2 0
2017-06-01 sku1 0 3 0
2017-06-02 sku1 6 4 0
2017-06-03 sku1 2 5 0
2017-05-29 sku2 4 0 24
2017-05-30 sku2 4 1 0
2017-05-31 sku2 0 2 0
2017-06-01 sku2 0 3 0
2017-06-02 sku2 0 4 0
2017-06-03 sku2 24 5 0
Here is the code I tried:
import pandas as pd
# 0 is Monday, 1 is Tuesday, etc
df["Weekday"] = df["Date"].dt.dayofweek
# create new column for the orders
df["Order"] = 0
min_days = 4
max_days = min_days + 7
for i in range(len(df)):
if df.loc[i, "Weekday"] == 0:
df.loc[i, "Order"] = df.loc[(df.Date >= df.loc[i, "Date"] + pd.to_timedelta(min_days, unit="D")) &
(df.Date < df.loc[i, "Date"] + pd.to_timedelta(max_days, unit="D")) &
(df.SKU == df.loc[i, "SKU"]), "received"].sum()
It seems to do the job, but in a slow way. Maybe someone can help me find a more pythonic/pandas approach to save some computation time.
Thanks for your help.