EDIT 2:
Considier the question closed.
After I gave the example in the first EDIT I have realised that I am senslessly looking through all of the columns looking for Highest value, since the High
column will always have the highest value and the following code is sufficient:
_high[f'Highest {n1}'] = price_df['High'].rolling(n1).max()
I am trying to get max from multiple columns with this code:
_high[f'Highest {n1}'] = price_df[['Open', 'Close', 'High', 'Low']].rolling(n1).max()
, but I am getting this error:
File "C:\Users\...\main.py", line 1282, in chop_zone
_high[f'Highest {n1}'] = price_df[['Open', 'Close', 'High', 'Low']].rolling(n1).max()
File "C:\Users\...\venv\lib\site-packages\pandas\core\frame.py", line 3968, in __setitem__
self._set_item_frame_value(key, value)
File "C:\Users\...\venv\lib\site-packages\pandas\core\frame.py", line 4098, in _set_item_frame_value
raise ValueError("Columns must be same length as key")
ValueError: Columns must be same length as key
Process finished with exit code 1
And I don't know why, how can I fix this?
EDIT:
What I expect the output to be:
Lets say I have the following dataframe (it is price_df[['Open', 'Close', 'High', 'Low']].tail(10)
)
Open Close High Low
5168 14010.0 14016.00 14024.05 14005.50
5169 14016.0 14018.00 14018.50 14007.50
5170 14018.0 14015.50 14021.50 14012.50
5171 14015.5 14007.00 14018.00 14004.50
5172 14007.0 14013.00 14013.50 13999.50
5173 14013.0 14007.00 14013.50 14002.00
5174 14007.0 14009.60 14017.60 14003.55
5175 14009.6 14013.60 14015.00 14004.60
5176 14013.6 14020.00 14021.60 14009.00
5177 14020.0 14015.55 14022.60 14013.60
So I expect a single column with the maximum value from the rolling window of size n1
along all columns.
For example if the rolling window would be 3 the first maximal value would be 14024.5
from column High
across all columns from index 5168
to 5170
, the next maximal value would be 14021.5 from the high column across from indices 5169
to 5171
.
CodePudding user response:
you are trying to assign a rolling maximum of a dataframe to a new column in the same dataframe which is not allowed. You can try this:
rolling_max = price_df[['Open', 'Close', 'High', 'Low']].rolling(n1).max()
#_high[f'Highest {n1}'] = rolling_max
_high[f'Highest {n1}'] = rolling_max['Open']