Search code examples
pythonpandasvalueerrorrolling-computation

The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). It is not because of operator "or"


vol_df value is as follows:

    stock transactions
0   6668624093
1   7000414730
2   7666161224
3   5882916889
4   6301315220
5   4240619512
6   5152771960
7   5604573262
8   5632000282
9   4141531366
10  5099018549
11  5269035601
12  5587861806
13  4516673303
14  4591419535
15  3914740328
16  4347751931
17  4745714862
18  4911766203
19  4901183056

but why i do avg_vol_df=pd.Series(vol_df).rolling(window=20).mean() I get:

"The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

please help me. Thank you very much.

I tried avg_vol_df=vol_df.rolling(20).mean()

I get NaN for answers

I tried avg_vol_df=vol_df.rolling(min_periods=1).mean()

I get scientifical notation like 6.66~~~~+09


Solution

  • The problem is that you're calling pd.Series on a DataFrame. If you have a dataframe with one column, you can use pd.DataFrame.squeeze() to change it to a series, but I would recommend just selecting the column you want:

    vol_df["stock transactions"].rolling(window=20).mean()
    

    or omitting it altogether to have the rolling be applied to all columns:

    vol_df.rolling(window=20).mean()
    

    The NaN values occur because a rolling window with size 20 needs at least 20 values, so your input contains only 1 such window.

    The scientific notation happens because computing the mean returns a floating point value, and your inputs are large enough to be formatted in scientific notation. You can customize this behavior if you'd like.