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]
Advertisement
Answer
Given your understanding that what you want to achieve is to have the BTC-USD data frame on the same trading day as the SPY trading day, the easiest way to do this is to reassign the BTC data frame with the SPY index.
BTC = BTC.reindex(SPY.index)
Full code:
import yfinance as yf import pandas as pd BTC = yf.download('BTC-USD', period = '2wk', interval = '1d') SPY = yf.download('SPY', start='2022-03-07', end='2022-03-21') BTC Open High Low Close Adj Close Volume Date 2022-03-07 38429.304688 39430.226562 37260.203125 38062.039062 38062.039062 28546143503 2022-03-08 38059.902344 39304.441406 37957.386719 38737.269531 38737.269531 25776583476 2022-03-09 38742.816406 42465.671875 38706.093750 41982.925781 41982.925781 32284121034 2022-03-10 41974.070312 42004.726562 38832.941406 39437.460938 39437.460938 31078064711 2022-03-11 39439.968750 40081.679688 38347.433594 38794.972656 38794.972656 26364890465 2022-03-12 38794.464844 39308.597656 38772.535156 38904.011719 38904.011719 14616450657 2022-03-13 38884.726562 39209.351562 37728.144531 37849.664062 37849.664062 17300745310 2022-03-14 37846.316406 39742.500000 37680.734375 39666.753906 39666.753906 24322159070 2022-03-15 39664.250000 39794.628906 38310.210938 39338.785156 39338.785156 23934000868 2022-03-16 39335.570312 41465.453125 39022.347656 41143.929688 41143.929688 39616916192 2022-03-17 41140.843750 41287.535156 40662.871094 40951.378906 40951.378906 22009601093 2022-03-18 40944.839844 42195.746094 40302.398438 41801.156250 41801.156250 34421564942 2022-03-19 41794.648438 42316.554688 41602.667969 42190.652344 42190.652344 19664853187 2022-03-20 42191.406250 42241.164062 41004.757812 41247.824219 41247.824219 20127946682 2022-03-21 41259.656250 41420.941406 40691.011719 41198.937500 41198.937500 22954921984 SPY Open High Low Close Adj Close Volume Date 2022-03-07 431.549988 432.299988 419.359985 419.429993 418.131012 137896600 2022-03-08 419.619995 427.209991 415.119995 416.250000 414.960876 164772700 2022-03-09 425.140015 429.510010 422.820007 427.410004 426.086304 116990800 2022-03-10 422.519989 426.429993 420.440002 425.480011 424.162292 93972700 2022-03-11 428.119995 428.769989 419.529999 420.070007 418.769043 95529600 2022-03-14 420.890015 424.549988 415.790009 417.000000 415.708557 95729200 2022-03-15 419.769989 426.839996 418.420013 426.170013 424.850159 106219100 2022-03-16 429.890015 435.679993 424.799988 435.619995 434.270874 144954800 2022-03-17 433.589996 441.070007 433.190002 441.070007 439.704010 102676900 2022-03-18 438.000000 444.859985 437.220001 444.519989 444.519989 106250400 BTC = BTC.reindex(SPY.index) BTC Open High Low Close Adj Close Volume Date 2022-03-07 38429.304688 39430.226562 37260.203125 38062.039062 38062.039062 28546143503 2022-03-08 38059.902344 39304.441406 37957.386719 38737.269531 38737.269531 25776583476 2022-03-09 38742.816406 42465.671875 38706.093750 41982.925781 41982.925781 32284121034 2022-03-10 41974.070312 42004.726562 38832.941406 39437.460938 39437.460938 31078064711 2022-03-11 39439.968750 40081.679688 38347.433594 38794.972656 38794.972656 26364890465 2022-03-14 37846.316406 39742.500000 37680.734375 39666.753906 39666.753906 24322159070 2022-03-15 39664.250000 39794.628906 38310.210938 39338.785156 39338.785156 23934000868 2022-03-16 39335.570312 41465.453125 39022.347656 41143.929688 41143.929688 39616916192 2022-03-17 41140.843750 41287.535156 40662.871094 40951.378906 40951.378906 22009601093 2022-03-18 40944.839844 42195.746094 40302.398438 41801.156250 41801.156250 34421564942