I haven't been able to find anything too similar to this I have OHLC data pulled from y-finance for multiple stocks. This results in a multi-index of columns of OHLC data and stock names
Python Script '''
import requests
import pandas as pd
import numpy as np
import yfinance as yf
from datetime import datetime, timedelta
N_DAYS_AGO = 15
now = datetime.now()
today = datetime(now.year,now.month,now.day, now.hour)
n_days_ago = today - timedelta(days=N_DAYS_AGO)
df = yf.download(['SPY','TLT'], start=n_days_ago, end=now, interval = "60m") #no error with 1 stock
ohlc_dict = {
'Adj Close':'last',
'Open':'first',
'High':'max',
'Low':'min',
'Close':'last',
'Volume':'sum'
}
df_sample = df.resample('W-FRI', closed='left').agg(ohlc_dict)
df_sample #error with 2 stocks
'''
The code above works without a single stock but fails when there are multiple stocks/ multi index columns.
I've tried stacking and unstacking but haven't found a good way to resample this data. What's the simplest path forward here?