如何使用 python pandas 结合销售预测、计划交货和当前库存来预测未来库存

How to combine a sales forecast, planned deliveries, and current inventory to project future inventory using python pandas

我有三个数据源:

销售预测 - 这是产品的未来预测销售:

Product Forecast_Quantity Forecast_Month
Product A 5 2021-02-28
Product B 6 2021-02-28
Product C 2 2021-02-28
Product A 5 2021-03-31
Product B 6 2021-03-31
Product C 2 2021-03-31
Product A 5 2021-04-30
Product B 6 2021-04-30
Product C 2 2021-04-30

计划交货(采购订单)- 计划交货时间:

Product Delivery_Quantity Delivery_Month
Product A 2 2021-02-28
Product B 4 2021-02-28
Product C 5 2021-02-28
Product A 8 2021-03-31
Product B 2 2021-03-31
Product C 4 2021-03-31
Product A 2 2021-04-30
Product B 6 2021-04-30
Product C 3 2021-04-30

当前库存 - 当前库存:

Product Inventory_Quantity Inventory_Month
Product A 20 2021-01-31
Product B 16 2021-01-31
Product C 21 2021-01-31

我想创建一个小程序,returns 一个数据框来预测我未来每种产品的库存。它应该需要上个月的结清库存加上交货数量并减去预测的销售额。 预期输出:

Product Inventory_Quantity Inventory_Month
Product A 20 2021-01-31
Product B 16 2021-01-31
Product C 21 2021-01-31
Product A 17 2021-02-28
Product B 14 2021-02-28
Product C 24 2021-02-28

这将是 18 个月的数据(但我刚刚为上述 table 使用了 2 个月的输出)。

我已经尝试了几种不同的方法,例如尝试 cusum() 或使用 for 循环,但我的逻辑并不正确。

这是我目前的代码,它将预测和交付组合成一个 'net' 数字:

import pandas as pd
import numpy as np

# Import CSV files as dataframe
fcs = pd.read_csv(r'forecast.csv')
inv = pd.read_csv(r'inventory.csv')
pos = pd.read_csv(r'purchase_orders.csv')

# Inner join to get a net position for each month (PO Qty - Fcs Qty)
net = pd.merge(pos, fcs, left_on=['SKU', 'Delivery_Date'], right_on=['SKU', 'Forecast_Date'])

# Create net
net['net'] = (net['PO_Quantity'] - net['Forecast_Quantity'])

我不确定现在生成与库存结合的新 table 的最佳方法?

使用concat垂直合并三个数据框。然后,fillna 库存列与其他列和 melt 数据框。从那里,您可以乘坐 cumsum:

df = (pd.concat([df1.assign(Forecast_Quantity=df1['Forecast_Quantity'] * -1)
                    .rename({'Forecast_Month' : 'Inventory_Month', 
                            'Forecast_Quantity' : 'Inventory_Quantity'}, axis=1),
                 df2.rename({'Delivery_Month' : 'Inventory_Month', 
                            'Delivery_Quantity' : 'Inventory_Quantity'}, axis=1),
                 df3]).sort_values(['Product', 'Inventory_Month']))
df['Inventory_Quantity'] = df.groupby('Product')['Inventory_Quantity'].cumsum()
df = (df.groupby(['Product', 'Inventory_Month'], as_index=False).last()
        .sort_values('Inventory_Month'))
df

Out[1]: 
      Product Inventory_Month  Inventory_Quantity
0   Product A      2021-01-31                20.0
4   Product B      2021-01-31                16.0
8   Product C      2021-01-31                21.0
1   Product A      2021-02-28                17.0
5   Product B      2021-02-28                14.0
9   Product C      2021-02-28                24.0
2   Product A      2021-03-31                20.0
6   Product B      2021-03-31                10.0
10  Product C      2021-03-31                26.0
3   Product A      2021-04-30                17.0
7   Product B      2021-04-30                10.0
11  Product C      2021-04-30                27.0