I have the following code:
df['FilterVol'] = np.NaN
df.loc[(df['Volume'] >= df['PrevVolume']), 'FilterVol'] = 1
df['DaysLast2Week'] = df['FilterVol'].rolling(14, min_periods=1).apply(value_counts)
and I'm getting the following future warning messages: FutureWarning: Calling float on a single element Series is deprecated and will raise a TypeError in the future. Use float(ser.iloc[0]) instead
FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
All this comes after upgrading my system from python 3.9 to 3.11 and a series of upgrades on various packages including pandas. My current version of pandas is 2.1.0. All columns in my table are dtype: float64. I've tried a few options but nothing seemed to work. Can anyone tell me how to modify this code to get rid of these warnings?
IIUC, you can simply Rolling.sum
to turn-off both FutureWarning
s in one shot :
FutureWarning
: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
FutureWarning
: Calling float on a single element Series is deprecated and will raise a TypeError in the future. Use float(ser.iloc[0]) instead
df["DaysLast2Week"] = df["FilterVol"].rolling(14, min_periods=1).sum()
Another variant, without using the intermediate column FilterVol
:
m = df["Volume"].ge(df["PrevVolume"])
df["DaysLast2Week"] = m.mask(~m).rolling(14, min_periods=1).sum()
Output :
print(df) # with your fixed approach
Volume PrevVolume FilterVol DaysLast2Week
0 117 392 NaN NaN
1 315 241 1.0 1.0
2 152 119 1.0 2.0
3 335 80 1.0 3.0
4 283 235 1.0 4.0
.. ... ... ... ...
95 183 159 1.0 5.0
96 346 231 1.0 6.0
97 262 240 1.0 6.0
98 278 296 NaN 6.0
99 274 144 1.0 7.0
[100 rows x 4 columns]
Used input :
np.random.seed(1)
df = pd.DataFrame({
"Volume": np.random.randint(80, 350, 100),
"PrevVolume": np.random.randint(65, 400, 100)}
)