This is killing me, I ve spent handful of hours trying to debug this on my own with no luck. Basically, I have a function1.py that uses Pandas to pulls in some csv files, join them according to my specifications, and is supposed to 1) print the resulting DF 2) write the DF to excel. The function1.py is its own module and is coded like this:
def getMerged(csv_files, output_name):
import pandas as pd
list_of_expiry_dfs = []
for csvfile in csv_files:
names = [ date , open , high , low , close , vol ] # this can also go outside the fn
df = pd.read_csv(csvfile, sep= ; , names=names)
df = df.set_index( date )
list_of_expiry_dfs.append(df)
x = 1
composite = list_of_expiry_dfs[0]
for each in list_of_expiry_dfs:
composite = composite.join(each, lsuffix=x-1, rsuffix=x, how= outer )
x = x + 1
yaxis =[]
for expiry in csv_files:
open = open ( + expiry[:6] + ")"
high = high ( + expiry[:6] + ")"
low = low ( + expiry[:6] + ")"
close = close ( + expiry[:6] + ")"
vol = vol ( + expiry[:6] + ")"
yaxis.append(open)
yaxis.append(high)
yaxis.append(low)
yaxis.append(close)
yaxis.append(vol)
composite.columns = yaxis
print(composite)
composite.to_excel(output_name, header=True)
At the bottom of the function.py, I can optionally choose to run this function, by provided some parameters as follows:
csv_files = [
BZ1219.csv ,
BZ0120.csv ,
BZ0220.csv ,
BZ0320.csv ,
BZ0420.csv
]
getMerged(csv_files, output_name= test.xlsx )
And it works just fine when run as the main executable. But that s not what I want to do. I ve identified a slew of issues I need help with.
First, I m totally unable to return "composite" outside of the function. I ve all types of solutions including return statements, global variables, setting the function equal to a variable, and also setting attributes. No matter what I do, I simply cannot assess composite outside of the function.
What I really want to do is to be able to import function1.py from another file and run it as I please. Aka function1.py containing code block 1 only (no code block 2). That other.py looks like this:
csv_files = [ BZ1219.csv , BZ0120.csv , BZ0220.csv , BZ0320.csv , BZ0420.csv ]
output_name = "test.xlsx"
from function1 import getMerged
getMerged(csv_files=csv_files, output_name=output_name)
This doesn t return an error, but also doesn t do anything. The closest I ve gotten is by adding the following code to code block 1. It does.. well, something. But it kicks the function into an infinite loop which eventually breaks down.
def getMerged(csv_files, output_name):
** insert the rest of first code block **
return composite
output = getMerged(csv_files=csvfiles, output_name=output_name)
After making a number of changes to other.py, I was able at one point to FINALLY get some output running in other.py with the above script, but again, it ran as an infinite loop until it broken down. I ve changed the code so many times I dont even remember what that code change to other.py was
Please help!