使用yfinance时持续出现「位置参数位于关键字参数之后」错误求助
错误原因及解决方法
错误根源是你调用yf.download()时,将print()这类无参数名的位置参数放在了start=、end=这类关键字参数之后,Python语法明确要求位置参数必须全部放在关键字参数之前,否则就会触发“位置参数位于关键字参数之后”的错误。
看你的代码片段:
stonks_df = yf.download('AMZN', print("Input start date:"), start=input(), print("Input end date:"), end=input(), group_by='column', progress=True)
这里print("Input start date:")是未指定参数名的位置参数,却紧跟在start=input()这个关键字参数前后,完全违反了参数传递的语法规则。
修正后的代码
把输入提示的print()语句移到yf.download()调用之外,提前获取起止日期再传入函数即可:
#imports import pandas as pd import yfinance as yf from yahoofinancials import YahooFinancials import time from datetime import datetime #setup today = datetime.today().strftime('%Y-%m-%d') #code #date format 'yyyy-mm-dd' print("Please input start and end in format", today) time.sleep(1) # 提前获取起止日期,将提示逻辑移出函数调用 print("Input start date:") start_date = input() print("Input end date:") end_date = input() stonks_df = yf.download('AMZN', start=start_date, end=end_date, group_by='column', progress=True) stonks_df.head() print(stonks_df) # 原代码变量名错误,需与定义的stonks_df保持一致
内容的提问来源于stack exchange,提问作者mmewtwosaysbye
相关产品推荐
相关产品推荐

