stock_c.reset_index(drop=True)
时间: 2024-03-04 08:54:00 浏览: 90
这段代码与之前的代码 `stock_c.reset_index()` 类似,也是将 DataFrame 的行索引转换成一列,但是这里多了一个参数 `drop=True`,表示将原来的行索引丢弃掉,不保留。这样做的好处是可以去掉行索引对数据分析的影响,使得数据更加干净整洁。如果不设置 `drop=True`,则会保留原来的行索引,转成一列后会出现两列索引,这样可能会增加数据处理的复杂度。
相关问题
Traceback (most recent call last): File "D:\hw_stock_2022\实盘选股策略\数据整理.py", line 238, in <module> all_stock_data.reset_index(inplace=True, drop=True) AttributeError: module 'datetime' has no attribute 'now'
这是一个python程序的错误输出,其中提到了文件 "D:\hw_stock_2022\实盘选股策略\数据整理.py" 的第 238 行发生了错误。错误类型是 AttributeError,错误信息是 "module datetime has no attribute now"。这意味着在程序中使用了 datetime 模块中没有的属性 "now"。
import pandas as pd df = pd.read_csv('stock_data.csv') 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 capital = 1000000 max_stock_per_day = 10 max_stock_value = 100000 start_date = '2020-01-01' end_date = '2023-01-01' df = df[(df['date'] >= start_date) & (df['date'] < end_date)] df = df.reset_index(drop=True) hold_stock = [] for i, row in df.iterrows(): if len(hold_stock) > 0: sell_stock = [] for stock in hold_stock: if i - stock['buy_day'] >= 3: capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002) sell_stock.append(stock) hold_stock = [stock for stock in hold_stock if stock not in sell_stock] df_today = df.loc[i:i+3] if i + 3 >= len(df): break if all(df_today['four_days_increase']) and all(df_today['three_days_decrease'].iloc[1:]): available_capital = capital available_stock = max_stock_per_day available_value = max_stock_value for j, stock_row in df_today.iterrows(): if available_capital > 0 and available_stock > 0 and available_value > 0: buy_qty = min(int(available_capital / (stock_row['close'] * 1.002)), available_stock, int(available_value / (stock_row['close'] * 1.002))) if buy_qty > 0: hold_stock.append({'buy_day': i, 'buy_price': stock_row['close'], 'buy_qty': buy_qty}) available_capital -= stock_row['close'] * buy_qty * 1.002 available_stock -= 1 available_value -= stock_row['close'] * buy_qty * 1.002 print('Final capital:', capital)让上述代码在jupyter里不报错
这段代码的错误可能是由于缺少依赖库或文件。请确保您已经安装了pandas库并且已经将stock_data.csv文件放在正确的位置。另外,您可能需要在Jupyter Notebook中使用`%matplotlib inline`来确保图表正确显示。以下是可能的修改:
```
import pandas as pd
%matplotlib inline
df = pd.read_csv('stock_data.csv')
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
capital = 1000000
max_stock_per_day = 10
max_stock_value = 100000
start_date = '2020-01-01'
end_date = '2023-01-01'
df = df[(df['date'] >= start_date) & (df['date'] < end_date)]
df = df.reset_index(drop=True)
hold_stock = []
for i, row in df.iterrows():
if len(hold_stock) > 0:
sell_stock = []
for stock in hold_stock:
if i - stock['buy_day'] >= 3:
capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002)
sell_stock.append(stock)
hold_stock = [stock for stock in hold_stock if stock not in sell_stock]
df_today = df.loc[i:i+3]
if i + 3 >= len(df):
break
if all(df_today['four_days_increase']) and all(df_today['three_days_decrease'].iloc[1:]):
available_capital = capital
available_stock = max_stock_per_day
available_value = max_stock_value
for j, stock_row in df_today.iterrows():
if available_capital > 0 and available_stock > 0 and available_value > 0:
buy_qty = min(int(available_capital / (stock_row['close'] * 1.002)), available_stock, int(available_value / (stock_row['close'] * 1.002)))
if buy_qty > 0:
hold_stock.append({'buy_day': i, 'buy_price': stock_row['close'], 'buy_qty': buy_qty})
available_capital -= stock_row['close'] * buy_qty * 1.002
available_stock -= 1
available_value -= stock_row['close'] * buy_qty * 1.002
print('Final capital:', capital)
```
阅读全文