Print last closing price of stock in python yfinance

  dataframe, floating-point, pandas, python, yfinance

I would like to download data with yfinance on python and later on print only the latest closing price of that stock as a float which is an issue because it is a <class 'pandas.core.frame.DataFrame'>.
So basically there are two problems:

  1. get the latest closing price
  2. and print it as a float

Since I don’t know how to begin with the problem of the closing price I will only show you my code trying to solve the float problem:

import yfinance as yf
from datetime import datetime, timedelta
import pandas as pd

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

ticker = "tsla"
start = datetime.now() - timedelta(days=2)
end = datetime.now()

price = yf.download(ticker, start=start, end=end, interval="1m")  # <class 'pandas.core.frame.DataFrame'>

print(float(price))

The error that I receive is:

    print(float(price))
TypeError: float() argument must be a string or a number, not 'DataFrame'

Thanks a lot for your help!

Source: Python Questions

LEAVE A COMMENT