Почему я получаю предупреждения «Среднее значение пустого среза» без NaN? ⇐ Python
-
Anonymous
Почему я получаю предупреждения «Среднее значение пустого среза» без NaN?
I've got a dataframe keptdata. I have a for loop to search through the rows of keptdata, and at one point need to average the previous values.
This is the relevant line in my code, that produces the warning:
avg = np.average(keptdata.iloc[i-500:i].price[keptdata.price != 0]) Here i is the variable that's being looped over in the for loop, price is a column in the dataframe, and I'm including the row in the average only if price is not zero.
Why am I getting a warning? Googling for the warning it seems to happen if there are NaNs in the dataframe, but I checked for that using keptdata.isnull.any().any() which returns false.
Another possibility is that if i-500 is negative, that might be causing problems, but I rewrote the for loop to start with i = 700 and I still get the warning.
Removing keptdata.price != 0 seems to resolve the issue, but that changes the line fundamentally.
Can the cause of the warning be fixed? Do I have any options other than suppressing the warning?
Источник: https://stackoverflow.com/questions/781 ... thout-nans
I've got a dataframe keptdata. I have a for loop to search through the rows of keptdata, and at one point need to average the previous values.
This is the relevant line in my code, that produces the warning:
avg = np.average(keptdata.iloc[i-500:i].price[keptdata.price != 0]) Here i is the variable that's being looped over in the for loop, price is a column in the dataframe, and I'm including the row in the average only if price is not zero.
Why am I getting a warning? Googling for the warning it seems to happen if there are NaNs in the dataframe, but I checked for that using keptdata.isnull.any().any() which returns false.
Another possibility is that if i-500 is negative, that might be causing problems, but I rewrote the for loop to start with i = 700 and I still get the warning.
Removing keptdata.price != 0 seems to resolve the issue, but that changes the line fundamentally.
Can the cause of the warning be fixed? Do I have any options other than suppressing the warning?
Источник: https://stackoverflow.com/questions/781 ... thout-nans