Search code examples
pythonpandasdataframenon-linear-regressionyfinance

How to extract the x and y values from yf.download


i have a simnple issue. I want to download Data from yfinance and store it in a Dataframe. That works. Now how can i additionally extract the X and Y Values, that are stored in that Dataframe? I mean, just from the fact, that the Data is plottable i conclude, that there are x and y values for every datapoint on the plot.

Here ist the simple code

import yfinance as yf
import matplotlib.pyplot as plt

stock = 'TSLA'
start = '2020-11-01'

df = yf.download(stock , start=start)

What i finally want to achieve ´would be to use the X and Y values to feed them into a polyfit function. In that way i am trying to do a regression on the pricechart data from a stock, to finally be able to take derivatives and apply some analysis on that function.

Anybody has a good idea?

I appreciate, thanks a lot, Benjamin


Solution

  • You can save the date and close price like this:

    X=df.index
    Y=df.Close
    

    And if you want to plot the Closeprice accordind to the date:

    df.reset_index().plot(x='Date', y='Close')
    

    If you want to use all the data except the close column to predict the Closeprice, you can keep them with:

    X=df.drop(columns='Close')