'Filtering out filtered dataframes which don’t have a minimum number of rows in a function

I have this function which is to be used iteratively in a for loop where I read the file and filter the data frame with a specified condition. I then use that data frame to create a regression model using Statsmodels where the coefficient is then extracted to create a price elasticity model.

With this model, I would like to not model and skip certain products in the loop which do not sell year round or at certain stores which means they have less than the 104 (2 Years) datapoints I am using to model.

This is a simplified version of my code which I have right now:

def coeff_model(x):
  file_name = …/C://…
  filtered_list = file_name[file_name[‘store_area’]==store_area]
  model = small.ols(formula=‘y ~ x’, data=filtered_list)
  res = model.fit()
  coefficient=res.params[‘x’]
return coefficient


for i in list:
  coeff_model(i)

At the moment I am getting this error when I encounter an iteration of the loop which gives an empty data frame:

zero-size array to reduction operation maximum which has no identity

Is there a way to solve my issue and go even further and be able to exclude any iterations without a full frame (104 rows)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source