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

Time:10-02

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

CodePudding user response:

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')

CodePudding user response:

So what worked for me in terms of extracting the Values from the Dataframe to a list is this proceedure:

import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np

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


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

nfx = []
nfy = []

for index, row in df.iterrows():
    
    x_data = nfx.append(index)
    y_data = nfy.append(row['Close'])
    
print(nfx)

However, i want to get the Timestamps as Integer, so that i can save them in a list and feed them into a Polyfit Function. How can i do that?

When i try :

import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np

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


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

nfx = []
nfy = []

for index, row in df.iterrows():
    
    x_data = nfx.append(np.asarray(index).astype(float))
    y_data = nfy.append(row['Close'])
    
print(nfx)

i get the following Error message:

TypeError: float() argument must be a string or a number, not 'Timestamp'
  • Related