On a pandas df of the below info
STOCK YR MONTH DAY PRICE
AAA 2022 1 1 10
AAA 2022 1 2 11
AAA 2022 1 3 10
AAA 2022 1 4 15
AAA 2022 1 5 10
BBB 2022 1 1 5
BBB 2022 1 2 10
BBB 2022 2 1 10
BBB 2022 2 2 15
I am trying to group by STOCK
and applying the following function
def next_pred(currents):
model = SimpleExpSmoothing(currents['PRICE'])
model_fit=model.fit()
next_price=model_fit.predict()
return (int(np.floor(next_price)))
I am using the following line of code:
future_price =df.groupby(['STOCK']).apply(lambda x: next_pred(x)).reset_index()
However, I am getting the below error:
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed
A print
of the df['PRICE']
from within the function, prints the data properly. Any suggestions on what might be causing the indexing error?
Thanks!
But I am not sure
CodePudding user response:
With the current sample data, no error. To reproduce the error, add this row for example
df.loc[9] = ['CCC',2022,1,1,30]
then the error is related to groups that have only one row. One way is then to remove these 1-row groups using duplicated
like:
res = (
df[df['STOCK'].duplicated(keep=False)]
.groupby(['STOCK']).apply(lambda x: next_pred(x))
.reset_index()
)