From 4a00ad79ddc508b16f15a58333475b0adb19ec6f Mon Sep 17 00:00:00 2001 From: DrPaprikaa <64958936+DrPaprikaa@users.noreply.github.com> Date: Thu, 10 Sep 2020 21:25:23 +0200 Subject: [PATCH] Update _post_process() in core.py @twopirllc, In the case where an indicator returns a `df`, I added an option in `_post_process()`, via `kwargs`, to append only specific columns to the data. It goes like this : ``` CustomStrategy = ta.Strategy( name="MyStrat", ta=[ {'kind':'bbands', 'params':(20,), 'col_numbers':(0,2)}, ] ) ``` or alternatively : `ao = df.ta.bbands(append=True, col_numbers=(0,2))` which can be usefull if the user needs only the upper and lower bbands for example. Thanks, DrPaprikaa --- pandas_ta/core.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas_ta/core.py b/pandas_ta/core.py index 4559635..aa0a48d 100644 --- a/pandas_ta/core.py +++ b/pandas_ta/core.py @@ -407,15 +407,18 @@ class AnalysisIndicators(BasePandasObject): return getattr(self,method)(*args, **kwargs)[0] - def _post_process(self, result, **kwargs) -> (Series, DataFrame): + def _post_process(self, result, **kwargs) -> (pd.Series, pd.DataFrame): """Applies any additional modifications to the DataFrame * Applies prefixes and/or suffixes * Appends the result to main DataFrame """ - if not isinstance(result, (Series, DataFrame)): + if not isinstance(result, (pd.Series, pd.DataFrame)): print(f"[X] Oops! The result was not a Series or DataFrame.") return self._df else: + # Append only specific columns to the dataframe (via 'col_numbers':(0,1,3) for example) + result = result.iloc[:,[int(n) for n in kwargs['col_numbers']]] if isinstance(result, pd.DataFrame) and 'col_numbers' in kwargs and kwargs['col_numbers'] is not None else result + # Add prefix/suffix and append to the dataframe self._add_prefix_suffix(result=result, **kwargs) self._append(result=result, **kwargs) return result @@ -1472,4 +1475,4 @@ class AnalysisIndicators(BasePandasObject): volume = self._get_column(kwargs.pop("volume", "volume")) result = vp(close=close, volume=volume, width=width, percent=percent, **kwargs) - return self._post_process(result, **kwargs) \ No newline at end of file + return self._post_process(result, **kwargs)