在python中使用重采样(';W';),sum()进行自定义基于句点的重采样时出错
我有一个数据帧(frame_combined_DF),看起来像这样。我需要根据为每个SKU提供的周时间进行自定义重采样在python中使用重采样(';W';),sum()进行自定义基于句点的重采样时出错,python,pandas,numpy,Python,Pandas,Numpy,我有一个数据帧(frame_combined_DF),看起来像这样。我需要根据为每个SKU提供的周时间进行自定义重采样 frame_combined_DF SKU Qty Time Time_W WY 2011-10-17 ABC 12.0 11.0 2 2012-01-16 ABC 20.0 11.0 2 2013-04-08 ABC 6.0
frame_combined_DF
SKU Qty Time Time_W
WY
2011-10-17 ABC 12.0 11.0 2
2012-01-16 ABC 20.0 11.0 2
2013-04-08 ABC 6.0 11.0 2
2013-12-02 ABC 2.0 11.0 2
2014-10-27 XYZ 1.0 21.0 3
下面是我的代码
for i in ids:
subset = frame_combined_DF.loc[frame_combined_DF.SKU==i]
subset.index=subset.WY
subset.sort_index(inplace=True)
period=subset.Time_W.unique().astype('int64')[0]
per=str(period)+'W'
df = subset.Qty.resample(per).sum()
new_df = {'WY':df.index, 'Qty':df.values,'SKU':i}
newdf = pd.DataFrame(new_df)
new_series=new_series.append(newdf)
运行此代码时出现以下错误
ValueError: Offset <0 * Weeks: weekday=6> did not increment date
从您的示例数据中,我看到WY是索引列 但请检查此列是否为datetime类型(不是字符串)。 如果不是,则运行
frame\u combined\u DF.index=pd.to\u datetime(frame\u combined\u DF.index)
另一点需要注意的是,newdf是一个数据帧,而不是一个系列,
因此,您应该将其附加到数据帧
第三点是不需要subset.index=subset.WY,因为
WY已经是索引了
最后一件事:您的示例没有定义新的_系列(在我的解决方案中)
我把它改成了结果)
因此,请将代码更改为:
result = pd.DataFrame()
for i in frame_combined_DF.SKU.unique():
subset = frame_combined_DF.loc[frame_combined_DF.SKU==i]
subset.sort_index(inplace=True)
period = subset.Time_W.unique().astype('int64')[0]
per = str(period) + 'W'
df = subset.Qty.resample(per).sum()
new_df = {'WY': df.index, 'Qty': df.values, 'SKU': i}
newdf = pd.DataFrame(new_df)
result = result.append(newdf, ignore_index=True)
它应该运行,至少在我的计算机上不会出现错误。您可以共享预期输出吗?更新了预期输出
result = pd.DataFrame()
for i in frame_combined_DF.SKU.unique():
subset = frame_combined_DF.loc[frame_combined_DF.SKU==i]
subset.sort_index(inplace=True)
period = subset.Time_W.unique().astype('int64')[0]
per = str(period) + 'W'
df = subset.Qty.resample(per).sum()
new_df = {'WY': df.index, 'Qty': df.values, 'SKU': i}
newdf = pd.DataFrame(new_df)
result = result.append(newdf, ignore_index=True)