Resample()返回不存在日期的不正确数字

我有一个这种格式的数据框:

Date Posted     Receipt Amount  Centre      Brand
07-10-2019      6000.0          Centre 1    Brand 1
07-05-2019      6346.66         Centre 2    Brand 1
03-01-2019      6173.34         Centre 1    Brand 2
11-06-2019      6000.0          Centre 1    Brand 2
13-09-2019      6346.66         Centre 3    Brand 1
07-11-2019      6098.34         Centre 4    Brand 1

我正在重新采样数据以进行时间序列预测:

df=pd.read_csv("File Directory")
df["Receipt Amount"] = df["Receipt Amount"].astype(float)
brands=list((pd.Series(df["Brand"].unique())).dropna())


df['Date Posted'] = pd.DatetimeIndex(df['Date Posted'])
df.index = df['Date Posted']
df=df.drop(["Date Posted"],axis=1)


for brand in brands:
    brand_filter=df['Brand']==brand
    brand_df=df[brand_filter]

    brand_df=brand_df[["Receipt Amount"]]

    brand_df=brand_df.resample('D').sum()   
    brand_df.reset_index(level=0,inplace=True)
    brand_df = brand_df.rename({'Date Posted': 'ds'},axis=1)
    brand_df = brand_df.rename({'Receipt Amount': 'y'},axis=1)

但是,这会将某些总和值返回为0,我知道这是错误的。 它还返回12月的几天的值,我再次知道这是错误的。 (所有数据都不是11月的最新数据)

这是完整的代码,所以我不确定在哪里出错。

zhangchuanlle 回答:Resample()返回不存在日期的不正确数字

我现在已经解决了这个问题,所以这是针对未来绝望的Google员工的解决方案。

以下者无法正确读取日期:

df['Date Posted'] = pd.DatetimeIndex(df['Date Posted'])

某些日期显示为dd / mm / yyyy,而其他日期则显示为mm / dd / yyyy。

要解决此问题,请在函数中添加dayfirst=True

df['Date Posted'] = pd.to_datetime(df['Date Posted'],dayfirst=True)
本文链接:https://www.f2er.com/2993909.html

大家都在问