仍然是python的新手。请原谅我问了一个菜鸟方程。使用 mftool 库帮助下载共同基金的资产净值数据。它给出了一个关键的错误'日期'。
将非常感谢识别和帮助解决错误。
输入:
from mftool import Mftool
mf = Mftool()
scheme_codes = mf.get_scheme_codes()
scheme_code_list = [x for x in scheme_codes.keys()]
def HistoricalNav(scheme_code_list, start_date, end_date):
assert (isinstance(scheme_code_list, list) is True), "Arguement scheme_code_list should be a list"
assert (isinstance(start_date, str) is True), "start_date must be a str in %d-%m-%Y format" # checks whether start date is present and is in correct format.
assert (isinstance(end_date, str) is True), "end_date must be a str in %d-%m-%Y format" # checks whether end date is present and is in correct format
main_df = pd.DataFrame() #empty dataframe
for schemes in scheme_code_list:
data = mf.get_scheme_historical_nav_for_dates(schemes, start_date, end_date) # requesting NAV data from the api.
df = pd.DataFrame(data['data'])
df['scheme_code'] = pd.Series([data['scheme_code'] for x in range(len(df.index))]) #adding Pandas Series(scheme_code) as a column in Pandas Dataframe.
df['scheme_name'] = pd.Series([data['scheme_name'] for x in range(len(df.index))]) #adding Pandas Series(scheme_name) as a column in Pandas Dataframe.
df = df.sort_values(by = 'date') # sorting the values of every Scheme code based on Date
main_df = main_df.append(df) # appending the data in the main_df dataframe.
main_df = main_df[['scheme_code', 'scheme_name', 'date', 'nav']] #creating names of dataframe columns
main_df.reset_index(drop = True, inplace = True)
return main_df #Returning the required Dataframe.
values_df = HistoricalNav(scheme_code_list = scheme_code_list[0:5], start_date= '01-05-2021', end_date= '01-05-2021')
values_df
**Error Message**
```Traceback (most recent call last):
File "C:/Users/am364971/Desktop/Python/Working/amfi.py", line 31, in <module>
values_df = HistoricalNav(scheme_code_list = scheme_code_list[0:5], start_date= '01-05-2021', end_date= '01-05-2021')
File "C:/Users/am364971/Desktop/Python/Working/amfi.py", line 22, in HistoricalNav
df = df.sort_values(by = 'date') # sorting the values of every Scheme code based on Date
File "C:\Users\am364971\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 5455, in sort_values
k = self._get_label_or_level_values(by, axis=axis)
File "C:\Users\am364971\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 1684, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'date'
我可以看到日期不是给定数据框中的列。如果有日期列,您可以通过键入 df.columns 来检查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句