Python Resample()返回不存在日期的错误数字
我有一个以下格式的数据框:Python Resample()返回不存在日期的错误数字,python,pandas,resampling,Python,Pandas,Resampling,我有一个以下格式的数据框: 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
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月)
这是完整的代码,所以我不确定我在哪里犯了错误。我现在已经解决了这个问题,所以这是未来绝望的谷歌用户的解决方案 以下人员未正确读入日期:
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)
任何遇到类似问题的人:df['Date Posted']=pd.DatetimeIndex(df['Date Posted'])没有正确读取日期。使用:df['Date Posted']=pd.to_datetime(df['Date Posted'],dayfirst=True)强制正确读取日期修复了问题