I am encountering an issue with the pandas library in Python after upgrading from Python 3.8 to Python 3.11, while using the same pandas version (1.5.3). When I attempt to use the 'rolling' method with 'bohman' as the window type, it results in a ValueError. The problematic code is as follows:
p_spp_mean = p_spp_in.rolling(
window, closed='left', center=True, win_type=self._smoothing_method
).mean()
self._smoothing_method = par.get('smoothing_method', 'bohman')
The error message I receive is:
raise ValueError(f"Invalid win_type {self.win_type}")
ValueError: Invalid win_type bohman
I tested the rolling function with another window type 'gaussian' on a simple DataFrame in a Jupyter notebook and it worked as expected:
import pandas as pd
import numpy as np
data = {
'date': pd.date_range(start='2023-01-01', periods=10, freq='D'),
'values': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
}
df = pd.DataFrame(data)
df['gaussian_rolling_sum'] = df['values'].rolling(window=2, win_type='gaussian').sum(std=3)
However, using 'bohman' as the window type now requires a workaround involving manually applying the window using scipy.signal:
import pandas as pd
import numpy as np
from scipy import signal
data = {
'date': pd.date_range(start='2023-01-01', periods=10, freq='D'),
'values': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
}
df = pd.DataFrame(data)
def bohman_window(x):
window = signal.get_window('bohman', len(x))
return np.sum(window * x) / np.sum(window)
df['bohman_rolling_sum'] = df['values'].rolling(window=3).apply(bohman_window)
I am unsure why this error occurs and how to adapt my code appropriately.
Has anyone experienced similar issues with window types in pandas when upgrading Python versions? How can I resolve the 'Invalid win_type' error without resorting to manual implementations for standard window types like 'bohman'?