使用yahoo finance在python中测试股息率
我正在尝试测试CSV文件中提供的股息率。其目的是检查CSV中给定日期各股票的股息率。因此,我试图使用yfinance library从yahoo finance收集股息率,目的是在csv文件中提供的利率旁边添加一列我收集的利率,以便我可以看到任何差异 这是我的代码示例:使用yahoo finance在python中测试股息率,python,dataframe,stock,yahoo-finance,price,Python,Dataframe,Stock,Yahoo Finance,Price,我正在尝试测试CSV文件中提供的股息率。其目的是检查CSV中给定日期各股票的股息率。因此,我试图使用yfinance library从yahoo finance收集股息率,目的是在csv文件中提供的利率旁边添加一列我收集的利率,以便我可以看到任何差异 这是我的代码示例: import pandas as pd import yfinance as yf from datetime import datetime,date ## Creating a dividend CSV file exa
import pandas as pd
import yfinance as yf
from datetime import datetime,date
## Creating a dividend CSV file example
tickers = ['ABP.AX', 'ABP.AX','AZJ.AX','AZJ.AX','BEN.AX','CIP.AX','CIP.AX',
'CIP.AX','CIP.AX','CMW.AX','CMW.AX','CMW.AX','CMW.AX']
Dates = ['12/30/2019 0:00','6/29/2020 0:00','2/24/2020 0:00','8/24/2020 0:00',
'3/6/2020 0:00','12/30/2019 0:00','3/30/2020 0:00','6/29/2020 0:00',
'9/29/2020 0:00','12/30/2019 0:00','3/30/2020 0:00','6/29/2020 0:00','9/29/2020 0:00']
rates = [0.094500,0.090500, 0.137000,0.137000,0.310000,0.046269,0.046269,
0.046269,0.042500,0.018750,0.018750,0.018750, 0.018750]
tickers = pd.Series(tickers)
Dates = pd.Series(Dates)
rates = pd.Series(rates)
frame = {"symbols" : tickers, 'Dates': Dates, "Amounts":rates}
dividends_csv = pd.DataFrame(frame)
## Now starting my Testing Process
stocks = (dividends_csv.symbols)
stocks = stocks.unique()
# My Desired Dates Range
start = '2019-10-1'
end = '2020-10-30'
data = pd.DataFrame()
## Collecting data from yahoo finance
for i in stocks:
series= yf.Ticker(i).dividends.loc[start:end]
data = pd.concat([data, series], axis=1)
data.columns = stocks
data.fillna(0)
# Transposing the data
data1 = data.T
## To have better view of the data set cearting MultiIndex data frame
data2= data1.stack()
data2 = data2.to_frame()
## Assigning Names to Indices
data2.index.names = ['symbol','Date']
# Name the column
data2.columns = ['checked']
# Adding columns using indices - This is additional work assuming may be columns will simplify than using multiIndex
data2['symbols'] = data2.index.get_level_values('symbol')
data2['Dates'] = data2.index.get_level_values('Date')
## Some of the answer on stackoverflow suggested sorting
dividends_csv.sort_values(by=['symbols', 'Dates'], inplace=True)
data2.sort_values(by=['symbols','Dates'], inplace=True)
# This through an error
dividends_csv.loc[ (dividends_csv['symbols'] == data2['symbols'] ) &
(dividends_csv['Dates'] == data2['Dates']), 'AuditRate3' ] = data2['checked']
## Merging - Merging is suplicating values
df = dividends_csv.merge(data2, on =["symbols"])
如果我运行这个:
# This through an error
dividends_csv.loc[ (dividends_csv['symbols'] == data2['symbols'] ) &
(dividends_csv['Dates'] == data2['Dates']), 'AuditRate3' ] = data2['checked']
我得到这个错误
raceback (most recent call last):
File "<input>", line 2, in <module>
File "H:\Education\Arrow\Arrow_reports\PortfolioApp\venv\lib\site-packages\pandas\core\ops\common.py", line 65, in new_method
return method(self, other)
File "H:\Education\Arrow\Arrow_reports\PortfolioApp\venv\lib\site-packages\pandas\core\ops\__init__.py", line 365, in wrapper
raise ValueError("Can only compare identically-labeled Series objects")
ValueError: Can only compare identically-labeled Series objects
我得到了重复的值
df
symbols Dates_x Amounts checked Dates_y
0 ABP.AX 12/30/2019 0:00 0.094500 0.094500 2019-12-30
1 ABP.AX 12/30/2019 0:00 0.094500 0.090500 2020-06-29
2 ABP.AX 6/29/2020 0:00 0.090500 0.094500 2019-12-30
3 ABP.AX 6/29/2020 0:00 0.090500 0.090500 2020-06-29
4 AZJ.AX 2/24/2020 0:00 0.137000 0.137000 2020-02-24
5 AZJ.AX 2/24/2020 0:00 0.137000 0.137000 2020-08-24
6 AZJ.AX 8/24/2020 0:00 0.137000 0.137000 2020-02-24
7 AZJ.AX 8/24/2020 0:00 0.137000 0.137000 2020-08-24
我将感谢任何帮助
谢谢
df
symbols Dates_x Amounts checked Dates_y
0 ABP.AX 12/30/2019 0:00 0.094500 0.094500 2019-12-30
1 ABP.AX 12/30/2019 0:00 0.094500 0.090500 2020-06-29
2 ABP.AX 6/29/2020 0:00 0.090500 0.094500 2019-12-30
3 ABP.AX 6/29/2020 0:00 0.090500 0.090500 2020-06-29
4 AZJ.AX 2/24/2020 0:00 0.137000 0.137000 2020-02-24
5 AZJ.AX 2/24/2020 0:00 0.137000 0.137000 2020-08-24
6 AZJ.AX 8/24/2020 0:00 0.137000 0.137000 2020-02-24
7 AZJ.AX 8/24/2020 0:00 0.137000 0.137000 2020-08-24