I have been using these two lines to get stock data:
df = yf.download(tickers, group_by="ticker")
d = {idx: gp.xs(idx, level=0, axis=1) for idx, gp in df.groupby(level=0, axis=1)}
from https://stackoverflow.com/a/66989947/21955590.
While it works fine, I can't figure out how to avoid getting the warning in the title.
How should this code be rewritten with df.T.groupby
without the axis=1
without causing an error?
Edit: In fact, you don't need groupby
:
d = {ticker: df[ticker] for ticker in df.columns.levels[0]}
There is a discussion about groupby(axis=1)
on github.
Another solution is to stack the ticker level:
d = {idx: gp.xs(idx, level=1) for idx, gp in df.stack(level=0).groupby(level=1)}
Output:
>>> d
{'AAPL': Open High Low Close Adj Close Volume
Date
1980-12-12 0.128348 0.128906 0.128348 0.128348 0.099449 469033600.0
1980-12-15 0.122210 0.122210 0.121652 0.121652 0.094261 175884800.0
1980-12-16 0.113281 0.113281 0.112723 0.112723 0.087343 105728000.0
1980-12-17 0.115513 0.116071 0.115513 0.115513 0.089504 86441600.0
1980-12-18 0.118862 0.119420 0.118862 0.118862 0.092099 73449600.0
... ... ... ... ... ... ...
2023-10-02 171.220001 174.300003 170.929993 173.750000 173.750000 52164500.0
2023-10-03 172.259995 173.630005 170.820007 172.399994 172.399994 49594600.0
2023-10-04 171.089996 174.210007 170.970001 173.660004 173.660004 53020300.0
2023-10-05 173.789993 175.449997 172.679993 174.910004 174.910004 48527900.0
2023-10-06 173.800003 177.990005 173.179993 177.490005 177.490005 57224100.0
[10795 rows x 6 columns],
'MSFT': Open High Low Close Adj Close Volume
Date
1986-03-13 0.088542 0.101563 0.088542 0.097222 0.060396 1.031789e+09
1986-03-14 0.097222 0.102431 0.097222 0.100694 0.062553 3.081600e+08
1986-03-17 0.100694 0.103299 0.100694 0.102431 0.063632 1.331712e+08
1986-03-18 0.102431 0.103299 0.098958 0.099826 0.062014 6.776640e+07
1986-03-19 0.099826 0.100694 0.097222 0.098090 0.060936 4.789440e+07
... ... ... ... ... ... ...
2023-10-02 316.279999 321.890015 315.179993 321.799988 321.799988 2.057000e+07
2023-10-03 320.829987 321.390015 311.209991 313.390015 313.390015 2.103350e+07
2023-10-04 314.029999 320.040009 314.000000 318.959991 318.959991 2.072010e+07
2023-10-05 319.089996 319.980011 314.899994 319.359985 319.359985 1.696560e+07
2023-10-06 316.549988 329.190002 316.299988 327.260010 327.260010 2.564550e+07
[9469 rows x 6 columns]}