Skip to content

线性插值法

python
import pandas as pd
import numpy as np

# 示例数据
data = {
    'store': ['A', 'A', 'A', 'B', 'B', 'B'],
    'product': ['X', 'X', 'X', 'Y', 'Y', 'Y'],
    'week': [1, 2, 3, 1, 2, 3],
    'weekly_sales': [0, 4, 5, 0, 0, 2],
    'weekly_inventory': [10, 110, 100, 50, 50, 50]
}

df = pd.DataFrame(data)

# 计算在库日数
df['inventory_days'] = 365 / (df['weekly_sales'] / df['weekly_inventory'] * 52)

# 处理无穷值
df['inventory_days'] = df['inventory_days'].replace([np.inf, -np.inf], np.nan)

# 按店铺 & 商品分组,按时间排序,并用前一周和后一周的均值填充
df.sort_values(['store', 'product', 'week'], inplace=True)
df['inventory_days'] = df.groupby(['store', 'product'])['inventory_days'].transform(lambda x: x.interpolate(method='linear', limit_direction='both'))

# 打印结果
print(df)