From 9ab6d08a306e8538098656c10d1bba6191d29c34 Mon Sep 17 00:00:00 2001 From: amr Date: Wed, 4 Oct 2023 09:04:32 -0700 Subject: [PATCH] Fix: enhance error handling for use_independent_variables --- src/pytimetk/core/expanding.py | 7 ++----- src/pytimetk/core/rolling.py | 10 +++++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/pytimetk/core/expanding.py b/src/pytimetk/core/expanding.py index 2075f598..d69ec6ae 100644 --- a/src/pytimetk/core/expanding.py +++ b/src/pytimetk/core/expanding.py @@ -181,14 +181,11 @@ def expanding_apply(func, df, min_periods): try: group_df[new_column_name] = group_df[value_col].expanding(min_periods=min_periods, **kwargs).apply(func, raw=True) except Exception as e: - specific_error_msg = "only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices" - if str(e) == specific_error_msg: try: # try independent variables incase user mistakenly did not set to True group_df[new_column_name] = expanding_apply(func, group_df, min_periods=min_periods) except: - raise - else: - raise + raise e + elif isinstance(func, str): new_column_name = f"{value_col}_expanding_{func}" diff --git a/src/pytimetk/core/rolling.py b/src/pytimetk/core/rolling.py index 35a064a1..fb695f1d 100644 --- a/src/pytimetk/core/rolling.py +++ b/src/pytimetk/core/rolling.py @@ -253,9 +253,13 @@ def rolling_apply_2(func, df, window_size, min_periods, center): if use_independent_variables: group_df[new_column_name] = rolling_apply_2(func, group_df, window_size, min_periods=min_periods, center=center) else: - # group_df[new_column_name] = rolling_apply(func, group_df[value_col]) - group_df[new_column_name] = group_df[value_col].rolling(window=window_size, min_periods=min_periods,center=center, **kwargs).apply(func, raw=True) - + try: + group_df[new_column_name] = group_df[value_col].rolling(window=window_size, min_periods=min_periods, center=center, **kwargs).apply(func, raw=True) + except Exception as e: + try: # try independent variables incase user mistakenly did not set to True + group_df[new_column_name] = rolling_apply_2(func, group_df, window_size, min_periods=min_periods, center=center) + except: + raise e elif isinstance(func, str): new_column_name = f"{value_col}_rolling_{func}_win_{window_size}"