# Buggy code
def reload(self):
    import pandas as pd

    if isinstance(self.source, pd.DataFrame):
        df = self.source
    elif isinstance(self.source, Path):
        filetype = getattr(self, "filetype", self.source.ext)
        if filetype == "tsv":
            readfunc = self.read_tsv
        elif filetype == "jsonl":
            readfunc = partial(pd.read_json, lines=True)
        else:
            readfunc = getattr(pd, "read_" + filetype) or vd.error(
                "no pandas.read_" + filetype
            )
        df = readfunc(str(self.source), **options.getall("pandas_" + filetype + "_"))

    # reset the index here
    if type(df.index) is not pd.RangeIndex:
        df = df.reset_index()

    self.columns = []
    for col in (c for c in df.columns if not c.startswith("__vd_")):
        self.addColumn(
            Column(
                col,
                type=self.dtype_to_type(df[col]),
                getter=self.getValue,
                setter=self.setValue,
            )
        )

    if self.columns[0].name == "index":  # if the df contains an index column
        self.column("index").hide()

    self.rows = DataFrameAdapter(df)
    self._selectedMask = pd.Series(False, index=df.index)
    if df.index.nunique() != df.shape[0]:
        vd.warning(
            "Non-unique index, row selection API may not work or may be incorrect"
        )


# Traceback (UnboundLocalError)
In [3]: import visidata

In [4]: visidata.view_pandas(None)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-4-5d0ae9b435d5> in <module>
----> 1 visidata.view_pandas(None)

~/opt/anaconda3/lib/python3.7/site-packages/visidata/loaders/_pandas.py in view_pandas(df)
318
319 def view_pandas(df):
--> 320     run(PandasSheet('', source=df))
321
322

~/opt/anaconda3/lib/python3.7/site-packages/visidata/mainloop.py in run(*sheetlist)
223     curses.use_env(True)
224
--> 225     ret = wrapper(cursesMain, sheetlist)
226
227     if ret:

~/opt/anaconda3/lib/python3.7/site-packages/visidata/mainloop.py in wrapper(f, *args)
213
214 def wrapper(f, *args):
--> 215     return curses.wrapper(setupcolors, f, *args)
216
217 def run(*sheetlist):

~/opt/anaconda3/lib/python3.7/curses/__init__.py in wrapper(*args, **kwds)
100             pass
101
--> 102         return func(stdscr, *args, **kwds)
103     finally:
104         # Set everything back to normal

~/opt/anaconda3/lib/python3.7/site-packages/visidata/mainloop.py in setupcolors(stdscr, f, *args)
210             curses.mouseEvents[getattr(curses, k)] = k
211
--> 212     return f(stdscr, *args)
213
214 def wrapper(f, *args):

~/opt/anaconda3/lib/python3.7/site-packages/visidata/mainloop.py in cursesMain(_scr, sheetlist)
234
235     for vs in sheetlist:
--> 236         vd.push(vs)
237
238     vd.status('Ctrl+H opens help')

~/opt/anaconda3/lib/python3.7/site-packages/visidata/sheets.py in push(vd, vs)
980         vd.allSheets.append(vs)
981
--> 982     vs.ensureLoaded()
983
984

~/opt/anaconda3/lib/python3.7/site-packages/visidata/basesheet.py in ensureLoaded(self)
187         if self.rows is UNLOADED:
188             self.rows = []  # prevent auto-reload from running twice
--> 189             return self.reload()   # likely launches new thread
190
191     def reload(self):

~/opt/anaconda3/lib/python3.7/site-packages/visidata/loaders/_pandas.py in reload(self)
118
119         # reset the index here
--> 120         if type(df.index) is not pd.RangeIndex:
121             df = df.reset_index()
122

UnboundLocalError: local variable 'df' referenced before assignment

# Fixed code
def reload(self):
    import pandas as pd

    if isinstance(self.source, pd.DataFrame):
        df = self.source
    elif isinstance(self.source, Path):
        filetype = getattr(self, "filetype", self.source.ext)
        if filetype == "tsv":
            readfunc = self.read_tsv
        elif filetype == "jsonl":
            readfunc = partial(pd.read_json, lines=True)
        else:
            readfunc = getattr(pd, "read_" + filetype) or vd.error(
                "no pandas.read_" + filetype
            )
        df = readfunc(str(self.source), **options.getall("pandas_" + filetype + "_"))
    else:
        try:
            df = pd.DataFrame(self.source)
        except ValueError as err:
            vd.fail("error building pandas DataFrame from source data: %s" % err)

    # reset the index here
    if type(df.index) is not pd.RangeIndex:
        df = df.reset_index()

    self.columns = []
    for col in (c for c in df.columns if not c.startswith("__vd_")):
        self.addColumn(
            Column(
                col,
                type=self.dtype_to_type(df[col]),
                getter=self.getValue,
                setter=self.setValue,
            )
        )

    if self.columns[0].name == "index":  # if the df contains an index column
        self.column("index").hide()

    self.rows = DataFrameAdapter(df)
    self._selectedMask = pd.Series(False, index=df.index)
    if df.index.nunique() != df.shape[0]:
        vd.warning(
            "Non-unique index, row selection API may not work or may be incorrect"
        )