[Tutor] yFinance/pandas?

Jim Byrnes jf_byrnes at comcast.net
Fri Nov 20 14:46:09 EST 2020


Using linux Mint20 and python 3.8

Wondering if anyone is familiar with yfinance/pandas?

I am writing a script to pull historical stock data from Yahoo Finance 
and insert it in a sqlite3 db. yfinance returns data as a pandas 
dataframe. I'm not that familiar with pandas but in the past I have been 
able to go on line and figure out how to extract the data I needed, but 
I am stuck now.

some test code:

  import yfinance as yf
import sqlite3

stock = 'AAPL'
ticker = yf.Ticker(stock)
hist = ticker.history(start='2018-01-01', end='2018-01-04')

print(hist)
#print(hist.iloc[0]['Name']) #['Name:']
print(hist.loc['2018-01-02'])  #['Close'])
#print(hist.iloc[0])

print(hist) outputs:

              Open   High    Low  Close  Volume  Dividends  Stock Splits
Date
2018-01-02  40.60  41.12  40.39  41.11  102223600          0          0
2018-01-03  41.17  41.65  41.03  41.10  118071600          0          0

print(hist.loc['2018-01-02']) outputs:

Open            4.060000e+01
High            4.112000e+01
Low             4.039000e+01
Close           4.111000e+01
Volume          1.022236e+08
Dividends       0.000000e+00
Stock Splits    0.000000e+00
Name: 2018-01-02 00:00:00, dtype: float64

If I retrieve a large number of rows the terminal will display the first 
few and last few rows and say x number of rows and 7 columns. Apparently 
pandas does not consider Date a column. I need to insert the date into 
the db along with the rest of the row.

I've tried using ['Name'] and ['Name:'] and [7] but get either key 
errors or index errors.

How can I retrieve the date?

Thanks,  Jim




More information about the Tutor mailing list