简体   繁体   English

如何使用 Pandas 在雅虎财经上选择匹配的日期/行和上一个日期/行?

[英]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.

相关问题 检查行的日期范围是否与 Python / Pandas Dataframe 中的任何先前行的日期范围重叠 - Check if row's date range overlap any previous rows date range in Python / Pandas Dataframe 熊猫将行分配给匹配行的索引 - Pandas Assign Row to matching row's index Pandas按日期逐行获取先前的数据帧 - Pandas get previous dataframe row by date 在 Pandas 中,如何使用精确日期查找行 - In Pandas, how to find row using exactly date pandas.DataFrame 使用 numpy.select() 和 .shift() 引用前一行的值 - pandas.DataFrame referencing previous row's value using numpy.select() and .shift() 使用 python pandas 的 Datareader 访问雅虎财经的关键统计网页 vs 默认雅虎财经的历史价格网页 - Using python pandas's Datareader for yahoo finance's key statistic web page vs default yahoo's finance's historial prices web page 使用 yahoo Finance 和 pandas 减少到仅行 totalRevenue 并重命名列名 - Reduce to only row totalRevenue and rename the colunmn names in years using yahoo finance and pandas 使用 Python 的 pandas,拆分日期并选择最近的日期 - Using Python's pandas, split the date and select the most recent date 怎么做,每行分组并在 python 中获取上一个日期的平均值 - How to do, each row grouping and get previous date's average in python 熊猫read_sql返回最后一行的日期 - pandas read_sql to return last row's date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM