path = 'Orders.csv'
df = pd.read_csv(path)
fill_price = df['Fill price']
time_placed = df['Time placed']
symbol = df['Symbol']
def update_worksheet():
# Remove useless row
i=0
useless_rows= []
while(i<=len(fill_price)-1):
if fill_price[i] == '--':
useless_rows.extend([i])
i += 1
# Reformat the time placed (i.e. time of orders)
i=0
while(i<=len(time_placed)-1):
try:
s = datetime.datetime.strptime(str(time_placed[i]), "%m/%d/%y %H:%M:%S%p")
df["Time placed"][i] = s.strftime("%Y-%m-%d")
except:
pass
i += 1
# Remove empty space in each symbol string
i=0
while(i<=len(symbol)-1):
try:
df["Symbol"][i] = df["Symbol"][i].replace(" ","")
except:
pass
i += 1
df = df.drop(useless_rows)
df.to_csv(path, index=False)
try:
update_worksheet()
except IndexError as err:
logger.info(" An error occurred. Please verify where it failed.")
Here is a code I have created, but it failed so far, but I can't really find out where the issue is located. Here is the traceback :
Traceback (most recent call last):
File "stock_tracking.py", line 60, in <module>
update_worksheet()
File "stock_tracking.py", line 55, in update_worksheet
df = df.drop(useless_rows)
UnboundLocalError: local variable 'df' referenced before assignment
How could I set that up? I tried many things, but I messed up with them.