import yfinance as yf
import pandas as pd
tickers = ['AAPL','NVDA','MSFT']
b_date = '2010-01-01'
e_date = '2020-12-31'
stock_data = yf.download(tickers,b_date,e_date)
print(stock_data)
我将开始日期和结束日期设置为2010-01-01和2020-12-31,但为什么结果只显示2010-01-04到2020-12-30的数据?
[*********************100%%**********************] 3 of 3 completed
Adj Close ... Volume
AAPL MSFT NVDA ... AAPL MSFT NVDA
Date ...
2010-01-04 6.487534 23.522572 4.240801 ... 493729600 38409100 80020400
2010-01-05 6.498749 23.530165 4.302728 ... 601904800 49749600 72864800
2010-01-06 6.395379 23.385765 4.330251 ... 552160000 58182400 64916800
2010-01-07 6.383555 23.142555 4.245389 ... 477131200 50559700 54779200
2010-01-08 6.425995 23.302160 4.254561 ... 447610800 51197400 47816800
... ... ... ... ... ... ... ...
2020-12-23 128.856812 215.676376 129.811371 ... 88223700 18699600 17914400
2020-12-24 129.850571 217.364548 129.656723 ... 54930100 10550600 9788400
2020-12-28 134.494797 219.521133 128.721237 ... 124486200 17933500 21256400
2020-12-29 132.704025 218.730698 129.152817 ... 121047300 17403200 17037200
2020-12-30 131.572495 216.320389 131.173416 ... 96452100 20272300 22539600
[2768 rows x 18 columns]
这对我来说没有任何意义,因为 2010-01-01 是星期三,2020-12-31 是星期四。
如何修复?
yifnance 0.2.28 熊猫2.0.3
这对我来说没有任何意义,因为 2010-01-01 是星期三,2020-12-31 是星期四。
你真的确定吗?
>>> pd.Timestamp('2010-01-01').strftime('%A') # New year, stock market is closed
'Friday'
>>> pd.Timestamp('2010-01-02').strftime('%A') # Weekend, stock market is closed
'Saturday'
>>> pd.Timestamp('2010-01-03').strftime('%A') # Weekend, stock market is closed
'Sunday'
>>> pd.Timestamp('2010-01-04').strftime('%A') # Monday, stock market is open
'Monday'
不包含最后一天。如果您想要这一天,您的结束日期应该是“2021-01-01”:
tickers = ['AAPL','NVDA','MSFT']
b_date = '2010-01-01'
e_date = '2021-01-01'
stock_data = yf.download(tickers,b_date,e_date)
print(stock_data)
# Output
Adj Close Close ... Open Volume
AAPL MSFT NVDA AAPL ... NVDA AAPL MSFT NVDA
Date ...
2010-01-04 6.487533 23.522570 4.240801 7.643214 ... 4.627500 493729600 38409100 80020400
2010-01-05 6.498750 23.530159 4.302727 7.656429 ... 4.605000 601904800 49749600 72864800
2010-01-06 6.395379 23.385761 4.330251 7.534643 ... 4.687500 552160000 58182400 64916800
2010-01-07 6.383558 23.142561 4.245389 7.520714 ... 4.695000 477131200 50559700 54779200
2010-01-08 6.425996 23.302160 4.254562 7.570714 ... 4.590000 447610800 51197400 47816800
... ... ... ... ... ... ... ... ... ...
2020-12-24 129.850601 217.364532 129.656708 131.970001 ... 130.372498 54930100 10550600 9788400
2020-12-28 134.494781 219.521133 128.721252 136.690002 ... 130.625000 124486200 17933500 21256400
2020-12-29 132.703995 218.730728 129.152802 134.869995 ... 129.250000 121047300 17403200 17037200
2020-12-30 131.572464 216.320419 131.173431 133.720001 ... 129.902496 96452100 20272300 22539600
2020-12-31 130.559021 217.042542 130.267868 132.690002 ... 131.365005 99116600 20942100 19242400
[2769 rows x 18 columns]