1

The following code works just fine. It creates a dataframe from a list of two variables and makes some calculations.

d = {'VGK':[1], 'VPL':[1]}
parser = lambda timestamp: pd.datetime.strptime(timestamp, '%Y-%m-%d')
etf_tickers = pd.DataFrame(data=d)
etfs = pd.DataFrame([])
for count,ticker in enumerate(etf_tickers):
 x = pd.read_csv('{}.csv'.format(ticker), parse_dates=[0], index_col='timestamp',date_parser=parser)
 x['{}_logdiff'.format(ticker)] = log(x[ticker]) - log(x[ticker].shift(1)) 
 if etfs.empty:
 etfs = x
 else:
 etfs= etfs.join(x, how='outer')

However when I move inside a function as follows it does not work. I don't get an error it just doesn't generate the dataframe.

d = {'VGK':[1], 'VPL':[1]}
parser = lambda timestamp: pd.datetime.strptime(timestamp, '%Y-%m-%d')
def create_frame_logdiffs(inlist,outframe):
 outtickers = pd.DataFrame(data=inlist)
 outframe = pd.DataFrame([])
 for count,ticker in enumerate(outtickers):
 x = pd.read_csv('{}.csv'.format(ticker), parse_dates=[0], index_col='timestamp',date_parser=parser)
 x['{}_logdiff'.format(ticker)] = log(x[ticker]) - log(x[ticker].shift(1)) #we use the natural log diff to compute retuns defined as the percentage change in price
 if outframe.empty:
 outframe = x
 else:
 outframe= outframe.join(x, how='outer')
create_frame_logdiffs(d,etfs)
asked Jan 10, 2019 at 0:48
3
  • I'm not sure why you have to pass etfs when you define outframe inside the function Commented Jan 10, 2019 at 1:01
  • @gameon67: You are right. I was wondering the same. Although in my answer I thought of leaving it out until I get a confirmation from the OP if my answer works for him in the current form before optimizing it Commented Jan 10, 2019 at 1:05
  • Bazingaa, your answer solves the problem. Gameon67, you are also both right on this. It was not necessary to pass etfs. I've optimized and removed. Thank you both! Commented Jan 10, 2019 at 20:46

1 Answer 1

1

The scope of the DataFrame is local within the function in which it is generated. Unless you return the DataFrame from your function to the place from where you made the call, you won't see anything. You need to put a return statement and save the output in some variable after calling the function. Something like

def create_frame_logdiffs(inlist,outframe):
 outtickers = pd.DataFrame(data=inlist)
 outframe = pd.DataFrame([])
 for count,ticker in enumerate(outtickers):
 x = pd.read_csv('{}.csv'.format(ticker), parse_dates=[0], index_col='timestamp',date_parser=parser)
 x['{}_logdiff'.format(ticker)] = log(x[ticker]) - log(x[ticker].shift(1)) #we use the natural log diff to compute retuns defined as the percentage change in price
 if outframe.empty:
 outframe = x
 else:
 outframe= outframe.join(x, how='outer')
 return outframe
outframe = create_frame_logdiffs(d,etfs)
# Do something with the outframe
answered Jan 10, 2019 at 0:56
Sign up to request clarification or add additional context in comments.

2 Comments

You are welcome :) You should also do the same for your future questions :) Have fun programming
Yeap. Thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.