Python Forum

Full Version: got an error while calculating a pct_change on a multiple tickers yfinace df
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am a newbie and will appreciate your help!

import pandas as pd
import yfinance as yf

list=['AAPL','MSFT','AMZN']
    
start="2020-1-1"
end="2020-12-31"

df = yf.download(list, start=start,end=end,group_by='Ticker', interval='1D')
df = df.stack(level=0).rename_axis(['Date', 'Ticker']).reset_index(level=1)
df['counter']=df.groupby(['Ticker']).cumcount()

df['NetCh']=df['Close'].pct_change()        # I know this is temporary wrong

for ticker in df.Ticker.unique():
    df['NetCh'][df.Ticker==ticker]=df[df.Ticker==ticker]['Close'].pct_change()

df
this is the error I get
Error:
<ipython-input-55-ec5dbe68cc4c>:16: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['NetCh'][df.Ticker==ticker]=df[df.Ticker==ticker]['Close'].pct_change()
SOLVED !!!

 df['NetCh']=df.groupby('Ticker').Close.pct_change()