[英]How to select matching date/row and previous date(s)/row(s) on Yahoo Finance using Pandas?
My purpose is to select dividend dates and previous date(s) in stock day data df of CSCO stock.我的目的是在 CSCO 股票的股票日数据 df 中选择股息日期和以前的日期。 I'm able to merge both datasets on index and ticker, however not figured out yet how to select previous date(s)/row(s) as well.我能够合并索引和代码上的两个数据集,但是还没有弄清楚如何选择以前的日期/行。
from yahoo_fin.stock_info import get_data, get_dividends
import numpy as np
import pandas as pd
stock_data = get_data('csco', start_date="10/01/2021", end_date="10/07/2021", index_as_date = True, interval="1d")
div_data = get_dividends('csco', "09/01/2021")
stock_data = stock_data.reset_index()
div_data = div_data.reset_index()
print(stock_data)
print(div_data)
Output:输出:
index open high ... adjclose volume ticker
0 2021-10-01 54.599998 55.410000 ... 54.770000 18338000 CSCO
1 2021-10-04 54.500000 54.680000 ... 54.230000 17084100 CSCO
2 2021-10-05 54.130001 55.029999 ... 54.689999 14135000 CSCO
3 2021-10-06 54.349998 54.380001 ... 53.939999 26339200 CSCO
index dividend ticker
0 2021-10-04 0.37 CSCO
Now, I'm able to merge based on index and ticker:现在,我可以根据索引和代码进行合并:
print(pd.merge(stock_data, div_data, on=['index', 'ticker'], how='inner'))
Output:输出:
index open high low ... adjclose volume ticker dividend
0 2021-10-04 54.5 54.68 53.950001 ... 54.23 17084100 CSCO 0.37
However, I want the previous date as well (so the day before the dividend pay date, where no dividends are shared out by the stock. My desired output:但是,我也想要上一个日期(所以在股息支付日期的前一天,股票没有分红。我想要的输出:
index open high low ... and close volume ticker dividend
0 2021-10-01 54.599998 55.410000 ... 54.770000 18338000 CSCO
0 2021-10-04 54.5 54.68 53.950001 ... 54.23 17084100 CSCO 0.37
Any suggestion on how to select previous date(s) as well, in addition to matching row?除了匹配行之外,还有什么关于如何选择以前的日期的建议吗?
Create a boolean mask by checking the dates in the stock_data
which are also present in div_data
, then remove the rows from stock_data
except the rows where the current and previous value in mask is True
.通过检查也存在于div_data
中的stock_data
的日期创建一个布尔掩码,然后从stock_data
删除除掩码中当前和先前值为True
的行之外的行。 Now you can perform the left merge to get the desired result现在您可以执行左合并以获得所需的结果
m = stock_data['index'].isin(div_data['index'])
stock_data[m | m.shift(-1)].merge(div_data, on=['index', 'ticker'], how='left')
index open high low close adjclose volume ticker dividend
0 2021-10-01 54.599998 55.41 54.040001 55.139999 54.77 18338000 CSCO NaN
1 2021-10-04 54.500000 54.68 53.950001 54.230000 54.23 17084100 CSCO 0.37
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.