This might be a relatively difficult question;
The scope of the code I want to write, is to automate the alignment of Dates that i pull from yfinance regarding BTC and S&P 500
since the S&P500 (SPY) is not traded on weekends, but BTC is, I want to automatically delete the columns of dates from BTC that fall on weekends (or days where the S&P isn't traded), to consistently align my 2 dataframes.
In this case I have 15 data rows in BTC, whereas I only have 10 in SPY
I only need data where the dates match
Does anyone have an idea how I could do that?
import yfinance as yf
import pandas as pd
BTC = pd.Dataframe = yf.download(tickers='BTC-USD', period = '2wk', interval = '1d')
SPY = yf.download('SPY', start='2022-03-07', end='2022-03-21')
print(BTC)
Open High ... Adj Close Volume
Date ...
2022-03-07 38429.304688 39430.226562 ... 38062.039062 28546143503
2022-03-08 38059.902344 39304.441406 ... 38737.269531 25776583476
2022-03-09 38742.816406 42465.671875 ... 41982.925781 32284121034
2022-03-10 41974.070312 42004.726562 ... 39437.460938 31078064711
2022-03-11 39439.968750 40081.679688 ... 38794.972656 26364890465
2022-03-12 38794.464844 39308.597656 ... 38904.011719 14616450657
2022-03-13 38884.726562 39209.351562 ... 37849.664062 17300745310
2022-03-14 37846.316406 39742.500000 ... 39666.753906 24322159070
2022-03-15 39664.250000 39794.628906 ... 39338.785156 23934000868
2022-03-16 39335.570312 41465.453125 ... 41143.929688 39616916192
2022-03-17 41140.843750 41287.535156 ... 40951.378906 22009601093
2022-03-18 40944.839844 42195.746094 ... 41801.156250 34421564942
2022-03-19 41794.648438 42316.554688 ... 42190.652344 19664853187
2022-03-20 42191.406250 42241.164062 ... 41247.824219 20127946682
2022-03-21 41259.656250 41420.941406 ... 41400.390625 23117129728
[15 rows x 6 columns]
print(SPY)
Open High ... Adj Close Volume
Date ...
2022-03-07 431.549988 432.299988 ... 418.131012 137896600
2022-03-08 419.619995 427.209991 ... 414.960876 164772700
2022-03-09 425.140015 429.510010 ... 426.086304 116990800
2022-03-10 422.519989 426.429993 ... 424.162292 93972700
2022-03-11 428.119995 428.769989 ... 418.769043 95529600
2022-03-14 420.890015 424.549988 ... 415.708557 95729200
2022-03-15 419.769989 426.839996 ... 424.850159 106219100
2022-03-16 429.890015 435.679993 ... 434.270874 144954800
2022-03-17 433.589996 441.070007 ... 439.704010 102676900
2022-03-18 438.000000 444.859985 ... 444.519989 106250400
[10 rows x 6 columns]