You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:53:16