#πŸ”’ ValueError with win_type 'bohman' in pandas rolling function after upgrading to Python 3.11

4 messages Β· Page 1 of 1 (latest)

ionic lintel
#

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'?

supple nebulaBOT
#

@ionic lintel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

supple nebulaBOT
#

@ionic lintel

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.