[Tutor] yFinance/pandas?

Jim Byrnes jf_byrnes at comcast.net
Fri Nov 20 21:04:11 EST 2020


On 11/20/20 3:09 PM, Dennis Lee Bieber wrote:
> On Fri, 20 Nov 2020 13:46:09 -0600, Jim Byrnes <jf_byrnes at comcast.net>
> declaimed the following:
> 
>> 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.
>>
> 
> 	I don't do pandas, but... From the yfinance source
> https://github.com/ranaroussi/yfinance/blob/master/yfinance/utils.py
> """
>      quotes = _pd.DataFrame({"Open": opens,
>                              "High": highs,
>                              "Low": lows,
>                              "Close": closes,
>                              "Adj Close": adjclose,
>                              "Volume": volumes})
> 
>      quotes.index = _pd.to_datetime(timestamps, unit="s")
> """
> 
> would imply that the date is NOT really part of the data frame itself; it
> is part of the INDEX to the data. Then going into
> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
> may provide hints. Off hand, I'd iterate over the .index values, retrieving
> the row for each such index, and then perform the SQL INSERT statement
> using the index and the row data.
> 

Turns out google was my friend. I didn't know what to google earlier but 
I tried  "yfinance return Date Column" and it gave me my answer.

I needed to change the "hist=" line to:

hist = ticker.history(start='2018-01-01', end='2018-01-04').reset_index()

That gave me a column I could get a date from and will be able to insert 
it along with the rest of the row in a db.

Thanks for you input.

Regards,  Jim




More information about the Tutor mailing list