I have a pandas DataFrame with a timestamp datetime index and values that correspond to each date. E.g., df = pd.DataFrame(['0.11', '0.07', '0.04', '-0.11', '-0.04', '0.08', '0.1'], index=['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05', '2017-01-06', '2017-01-07'], columns=['values'])
.
I would like to create an additional column (let's call it 'new_value'
) based on the present and historical values of the above dataframe.
The logic should be:
- if a value is greater than or equal to 0.1, 'new_value' should be set to -1,
- once 'new_value' is set to -1, it should remain -1 until a value less than or equal to 0.05 is registered,
- if a value is less than or equal to -0.1, 'new_value' should be set to +1,
- once 'new_value' is set to +1, it should remain +1 until a value greater than or equal to -0.05 is registered,
- otherwise 'new_value' is equal to 0
I have tried multiple solutions, but can't seem to solve this problem. E.g.,
new_frame = pd.DataFrame(np.zeros(len(df.index),index=df.index,columns=['new_value'])
for date in df.index:
if df['value'][date.strftime('%Y-%m-%d')] > 0.1:
new_frame.set_value(date.strftime("%Y-%m-%d"),'new_value',-1)
But I receive the error: 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'
If I then change the third line to:
if df['value'][date.strftime('%Y-%m-%d').item() > 0.1:
I receive the error: 'ValueError: can only convert an array of size 1 to a Python scalar'