I have a problem with iterating through my numpy array. I have two functions (f and g) and the Timestamp for the x-axis. First, I determined the interception of f and g. Then I wanted to counter how many times the interception happens, while the values of my function f are greater than 0. I got the following error after running the code. (Also I tried f.all() ):
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Here is the code:
import pandas as pd
import numpy as np
df = pd.read_csv("Sensor2.csv", delimiter=";", parse_dates=True)
df.drop('x', axis=1, inplace=True)
df.drop('y', axis=1, inplace=True)
df["100MA"] = pd.rolling_mean(df["z"], 100, min_periods=1)
df["200MA"] = pd.rolling_mean(df["z"], 200, min_periods=1)
x = np.array(df['Timestamp'])
f = np.array(df['100MA'])
g = np.array(df['200MA'])
index = np.argwhere((np.diff(np.sign(f - g)) != 0)).reshape(-1).astype(int)
counter = 0
for i in np.nditer(index):
if f > 0:
counter = counter +1
print counter
Thank you for your help!
nditer. Iterate directly.