Resample() 为不存在的日期返回不正确的数字
Resample() returning incorrect figures for non-existent dates
我有一个这种格式的数据框:
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)
然而,这 returns 一些总和值为 0,我知道这是错误的。
此外,它 returns 值表示 12 月的天数,我再次知道这是错误的。 (所有数据均不晚于11月)
这是完整的代码,所以我不确定我在哪里犯了错误。
我现在已经解决了这个问题,所以这是为未来绝望的 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)
我有一个这种格式的数据框:
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)
然而,这 returns 一些总和值为 0,我知道这是错误的。 此外,它 returns 值表示 12 月的天数,我再次知道这是错误的。 (所有数据均不晚于11月)
这是完整的代码,所以我不确定我在哪里犯了错误。
我现在已经解决了这个问题,所以这是为未来绝望的 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)