I'm trying to pull information for 5y of ticker history(where applicable) from a series of stocks and save it to a csv so I only have to do this step once (can load the 5 year data locally and just update daily to it for the values I end up following). however it keeps throwing an error on the write side and not sure how to correct as all sources I've seen are either cryptic(as in the official documentation) or don't work (other solutions here and a few other sites I can't remember). From the error it seems i need to reshape the data however I'm not quite sure how to go about doing that, and researching it just left me more confused.
Code and Error are as follows.
import yfinance as yf
import pandas as pd
data = pd.DataFrame()
foo = yf.Ticker('GRAB')
temp = pd.DataFrame([foo.history('5y')])
temp.to_csv('out.csv')
getting error:
Traceback (most recent call last): File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\stock 2.py", line 10, in temp = pd.DataFrame([foo.history('5y')]) File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 762, in init mgr = ndarray_to_mgr( File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 329, in ndarray_to_mgr values = _prep_ndarraylike(values, copy=copy_on_sanitize) File "C:\Users\jmshe\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 583, in _prep_ndarraylike raise ValueError(f"Must pass 2-d input. shape={values.shape}") ValueError: Must pass 2-d input. shape=(1, 561, 7)
please keep in mind this is just a test code before writing the full thing so I don't have to constantly poll yahoo 1000 times during testing
Attempted pandas.shape()
but unsure of exact syntax
foo.history('5y')
already returns a DataFrame so you don't need to create one:
import yfinance as yf
import pandas as pd
data = pd.DataFrame()
foo = yf.Ticker('GRAB')
temp = foo.history('5y')
# or temp = yf.Ticker('GRAB').history('5y')
temp.to_csv('out.csv')
Output:
>>> temp
Open High Low Close Volume Dividends Stock Splits
Date
2020-12-01 11.89 11.89 11.89 11.89 500 0 0
2020-12-02 12.48 12.48 11.82 11.82 1000 0 0
2020-12-03 11.82 11.90 11.80 11.90 14100 0 0
2020-12-04 12.99 13.98 11.50 12.40 15500 0 0
2020-12-07 13.15 14.00 12.20 12.55 14500 0 0
... ... ... ... ... ... ... ...
2023-02-16 3.63 3.73 3.56 3.67 20078600 0 0
2023-02-17 3.65 3.69 3.43 3.48 17590000 0 0
2023-02-21 3.47 3.52 3.38 3.40 12430000 0 0
2023-02-22 3.45 3.55 3.35 3.50 18951100 0 0
2023-02-23 3.55 3.57 3.15 3.21 26774253 0 0