import pandas as pd import matplotlib.pyplot as plt import pyodbc Setup connection to…
- User
import pandas as pd import matplotlib.pyplot as plt import pyodbc
Setup connection to the database
conn_str = r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\LIQI\Documents\microcapstrategy.accdb" try: conn = pyodbc.connect(conn_str)
# 执行查询 query = "SELECT TradingDay, SecuCode, TurnoverRateRM, YearVolatilityByDay, Prc2bk FROM Portfolio" df = pd.read_sql(query, conn) # 关闭连接finally: conn.close()
# 将相关列转换为float类型 df['TurnoverRateRM'] = df['TurnoverRateRM'].astype(float) df['YearVolatilityByDay'] = df['YearVolatilityByDay'].astype(float) df['Prc2bk'] = df['Prc2bk'].astype(float) # 统计不同tradingdate对应的每个stock ticker的因子值 statistics = df.groupby('TradingDay').agg({'TurnoverRateRM': ['mean', 'median', lambda x: x.quantile(0.1), lambda x: x.quantile(0.25), \ lambda x: x.quantile(0.75), lambda x: x.quantile(0.9)],'YearVolatilityByDay': ['mean', 'median', lambda x: x.quantile(0.1),\ lambda x: x.quantile(0.25), lambda x: x.quantile(0.75), lambda x: x.quantile(0.9)],'Prc2bk': ['mean', 'median',\ lambda x: x.quantile(0.1), lambda x: x.quantile(0.25), lambda x: x.quantile(0.75),lambda x: x.quantile(0.9)]}) # 绘制时间序列折线图 dates = statistics.index.tolist() plt.figure(figsize=(12, 6)) # 绘制因子A的时间序列折线图 plt.subplot(3, 1, 1) plt.plot(dates, statistics[('TurnoverRateRM', 'mean')], label='Mean') plt.plot(dates, statistics[('TurnoverRateRM', 'median')], label='Median') plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_0>'')], label='10% Quantile') plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_1>'')], label='25% Quantile') plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_2>'')], label='75% Quantile') plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_3>'')], label='90% Quantile') plt.ylabel('近月换手率') plt.legend() # 绘制因子B的时间序列折线图 plt.subplot(3, 1, 2) plt.plot(dates, statistics[('YearVolatilityByDay', 'mean')], label='Mean') plt.plot(dates, statistics[('YearVolatilityByDay', 'median')], label='Median') plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_0>'')], label='10% Quantile') plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_1>'')], label='25% Quantile') plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_2>'')], label='75% Quantile') plt.plot(dates, statistics[('YearVolatilityByDay', ''<lambda_3>'')], label='90% Quantile') plt.ylabel('年化波动率') plt.legend() # 绘制因子C的时间序列折线图 plt.subplot(3, 1, 3) plt.plot(dates, statistics[('Prc2bk', 'mean')], label='Mean') plt.plot(dates, statistics[('Prc2bk', 'median')], label='Median') plt.plot(dates, statistics[('Prc2bk', ''<lambda_0>'')], label='10% Quantile') plt.plot(dates, statistics[('Prc2bk', ''<lambda_1>'')], label='25% Quantile') plt.plot(dates, statistics[('Prc2bk', ''<lambda_2>'')], label='75% Quantile') plt.plot(dates, statistics[('Prc2bk', ''<lambda_3>'')], label='90% Quantile') plt.ylabel('改良市净率') plt.legend() plt.xlabel('建仓日') plt.tight_layout() plt.show() 我写了一段代码 但是运行出错了 帮我看看怎么改 我把错误信息贴给你 - Assistant
请提供代码出错的具体错误信息,以便我能够帮助您进行修正。
- User
- Assistant
该错误是由于pandas版本更新导致的。在最新版本的pandas中,不再支持多维索引操作。
为了解决这个问题,您可以使用
.values将DataFrame列转换为NumPy数组,然后再进行索引操作。请尝试以下修改:将以下代码行:
plt.plot(dates, statistics[('TurnoverRateRM', 'mean')], label='Mean')修改为:
plt.plot(dates, statistics[('TurnoverRateRM', 'mean')].values, label='Mean')请按照上述步骤将所有的类似代码行进行修改,然后重新运行程序看看是否能够正常运行。
- User
NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_13004/227870123.py in 36 plt.plot(dates, statistics[('TurnoverRateRM', 'mean')].values, label='Mean') 37 plt.plot(dates, statistics[('TurnoverRateRM', 'median')].values, label='Median') ---> 38 plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_0>'')].values, label='10% Quantile') 39 plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_1>'')].values, label='25% Quantile') 40 plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_2>'')].values, label='75% Quantile')
NameError: name 'lambda_0' is not defined
- Assistant
对不起,我犯了一个错误。由于lambda函数无法直接作为列名,在索引操作中引发了NameError。请修改以下代码:
将以下代码行:
plt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_0>'')].values, label='10% Quantile')修改为:
plt.plot(dates, statistics[('TurnoverRateRM', '<lambda_0>')].values, label='10% Quantile')请按照上述步骤将所有的类似代码行进行修改,然后重新运行程序看看是否能够正常运行。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.