import pandas as pd import matplotlib.pyplot as plt import pyodbc import xlwings as xw…
- User
import pandas as pd import matplotlib.pyplot as plt import pyodbc import xlwings as xw
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) # 关闭连接 conn.close() # 统计不同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() 运行以后返回 File "C:\Users\LIQI\AppData\Local\Temp/ipykernel_13004/3428647285.py", line 63 plt.show() ^SyntaxError: unexpected EOF while parsing 请问问题在哪里
- Assistant
- User
我的缩进是正确的呀
- Assistant
非常抱歉,我之前的回答有误判断。在重新检查代码后,您的缩进是正确的。
根据您提供的代码,我没有找到导致语法错误的明显问题。这意味着可能有其他部分的代码或其他文件中的问题。建议您检查代码的其他部分,确保没有任何其他语法错误或奇怪的标点符号。
另外,仍然有可能出现其他问题,例如缺少某些库或数据表中存在不完整或不规范的数据。您可以针对这些可能性进行检查,以确定问题的根本原因。
如果仍然存在问题,建议您提供更多的代码细节或错误信息,以便更详细地分析并帮助解决问题。
- User
应该是漏掉了finally: conn.close()
- Assistant
- User
好的 现在应该还需要一段代码 我的数据库里数据都保存成了string 而计算lamda函数应该要转成float
- Assistant
如果您的数据库中的数据保存为字符串,而计算lambda函数需要将其转换为float类型,您可以使用
.astype()方法将数据列转换为相应的数据类型。以下是更新后的代码示例,将相关列的数据类型转换为float:
import pandas as pd import matplotlib.pyplot as plt import pyodbc import xlwings as xw # 设置与数据库的连接 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) # 关闭连接 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