简体   繁体   中英

Resampling - Pandas Python

I'm trying to forecast retail sales time series using python. Data set contains the following attributes, [Order date Ship mode Segment Customer ID Category Product ID Product name Sales Quantity Discount Profit]. Here I have imported the dataset and I have indexed with time series data

prdct1 = prdct1.groupby('Order Date')['Sales'].sum().reset_index()
prdct1 = prdct1.set_index('Order Date')
prdct1.index

Date time index has been displayed for the above code. When I try to resample the average daily sales value for start of each month as a timestamp,

y = prdct1['Sales'].resample('MS').mean()

I'm getting the following error,

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

Could anyone help?

I think you need DatetimeIndex by converting column Order Date before your solution, also reset_index with set_index should be omit:

prdct1['Order Date'] = pd.to_datetime(prdct1['Order Date'])
prdct1 = prdct1.groupby('Order Date')['Sales'].sum()

And then:

y = prdct1.resample('MS').mean().reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM