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)
-
I'm not sure why you have to pass etfs when you define outframe inside the functioniv67– iv672019年01月10日 01:01:53 +00:00Commented 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 itSheldore– Sheldore2019年01月10日 01:05:25 +00:00Commented 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!j miranda– j miranda2019年01月10日 20:46:47 +00:00Commented Jan 10, 2019 at 20:46
1 Answer 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
Sheldore
39.2k9 gold badges63 silver badges76 bronze badges
lang-py