UnboundLocalError:局部变量' df'在赋值之前引用 - Python

时间:2018-02-18 15:09:00

标签: python

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.")

这是我创建的代码,但到目前为止失败了,但我无法找到问题所在的位置。这是追溯:

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

我该如何设置?我尝试了很多东西,但是我搞砸了。

0 个答案:

没有答案