df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1 修改0 is not in range
时间: 2024-02-29 19:51:55 浏览: 52
这段代码出现了错误,可能是由于数据的长度不足以进行指定窗口大小的滚动计算,导致出现了“修改0 is not in range”的错误提示。
你可以添加一些判断,确保数据的长度足够进行计算,例如:
```
if len(df) >= 4:
df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1
if len(df) >= 3:
df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1
```
这样就可以避免出现“修改0 is not in range”的错误提示了。
相关问题
df = pd.read_csv('stock_data.csv') if len(df) >= 4: df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 if len(df) >= 3: df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1增加如果窗口大小小于或等于零则不操作
可以在代码中增加对窗口大小的判断,如果窗口大小小于等于 0,就不进行滑动窗口计算。修改后的代码如下:
```
df = pd.read_csv('stock_data.csv')
if len(df) >= 4 and window_size > 0:
df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1
if len(df) >= 3 and window_size > 0:
df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1
```
其中,window_size 为窗口大小,可以在代码中对其进行设置。如果窗口大小小于等于 0,就不会进行滑动窗口计算。
df = pd.read_csv('stock_data.csv') if len(df) >= 4 and window_size > 0: df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 if len(df) >= 3 and window_size > 0: df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1增加定义window_size
可以在代码中定义 window_size 变量,如下:
```
window_size = 4 # 定义窗口大小
df = pd.read_csv('stock_data.csv')
if len(df) >= window_size and window_size > 0:
df['four_days_increase'] = df['close'].rolling(window=window_size).apply(lambda x: all(x[i] < x[i+1] for i in range(window_size-1))) * 1
if len(df) >= window_size-1 and window_size > 0:
df['three_days_decrease'] = df['close'].rolling(window=window_size-1).apply(lambda x: all(x[i] > x[i+1] for i in range(window_size-2))) * 1
```
其中,window_size 的值可以根据需要进行调整。如果窗口大小小于等于 0,就不会进行滑动窗口计算。
阅读全文