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)
code# 执行查询 query = "SELECT TradingDay, SecuCode, TurnoverRateRM, YearVolatilityByDay, Prc2bk FROM Portfolio" df = pd.read_sql(query, conn) # 关闭连接finally: conn.close()
code - Assistant
请提供代码出错的具体错误信息,以便我能够帮助您进行修正。
- User
- Assistant
该错误是由于pandas版本更新导致的。在最新版本的pandas中,不再支持多维索引操作。
为了解决这个问题,您可以使用
.values将DataFrame列转换为NumPy数组,然后再进行索引操作。请尝试以下修改:将以下代码行:
pythonplt.plot(dates, statistics[('TurnoverRateRM', 'mean')], label='Mean')修改为:
pythonplt.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。请修改以下代码:
将以下代码行:
pythonplt.plot(dates, statistics[('TurnoverRateRM', ''<lambda_0>'')].values, label='10% Quantile')修改为:
pythonplt.plot(dates, statistics[('TurnoverRateRM', '<lambda_0>')].values, label='10% Quantile')请按照上述步骤将所有的类似代码行进行修改,然后重新运行程序看看是否能够正常运行。