Python Forum
got an error while calculating a pct_change on a multiple tickers yfinace df
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
got an error while calculating a pct_change on a multiple tickers yfinace df
#1
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()
Reply
#2
SOLVED !!!

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


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020