title: Error in Python: name 'df' is not defined
body: <p>Im new in python, many hours googled and search here in stackoverflow. But still have problem with my code. I need run <em>foo</em> function in time interval, and fill dataframe (eg every second add timestamp to df). I'm still coming across a error message:  <strong>NameError:</strong> <em>name 'df' is not defined</em></p>
  
  <p>Thank you for your answer.</p>
  
  <p>Code:</p>
  
  <pre><code>    WAIT_TIME_SECONDS = 1
  
  
  class ProgramKilled(Exception):
      pass
  
      df = pd.DataFrame([])
  
  ############################################################
  ############################################################
  def foo():
  
      global df
      time1 = str(datetime.now())
      time2 = time1.replace(":", "_")
      print(time2)
  
      df2 = pd.DataFrame(data= {
      'timestamp': [time2],
      'cas_zapas_1_text': ['cas']
      })
  
      df = df.append(df2)
  
  ############################################################
  ############################################################
  def signal_handler(signum, frame):
      raise ProgramKilled
  
  class Job(threading.Thread):
      def __init__(self, interval, execute, *args, **kwargs):
          threading.Thread.__init__(self)
          self.daemon = False
          self.stopped = threading.Event()
          self.interval = interval
          self.execute = execute
          self.args = args
          self.kwargs = kwargs
  
      def stop(self):
                  self.stopped.set()
                  self.join()
      def run(self):
              while not self.stopped.wait(self.interval.total_seconds()):
                  self.execute(*self.args, **self.kwargs)
  
  if __name__ == "__main__":
      signal.signal(signal.SIGTERM, signal_handler)
      signal.signal(signal.SIGINT, signal_handler)
      job = Job(interval=timedelta(seconds=WAIT_TIME_SECONDS), execute=foo)
      job.start()
  
      while True:
            try:
                time.sleep(1)
            except ProgramKilled:
                print("Program killed: running cleanup code")
                job.stop()
                break
  </code></pre>
  
  <p>This is resultof script:</p>
  
  <pre><code>runfile('C:/Users/Miro/.spyder-py3/untitled8.py', wdir='C:/Users/Miro/.spyder-py3')
  Exception in thread Thread-8:
  Traceback (most recent call last):
    File "C:\Users\Miro\Anaconda3\envs\spyder_4_0_1\lib\threading.py", line 932, in _bootstrap_inner
      self.run()
    File "C:\Users\Miro\.spyder-py3\untitled8.py", line 90, in run
      self.execute(*self.args, **self.kwargs)
    File "C:\Users\Miro\.spyder-py3\untitled8.py", line 68, in foo
      df = df.append(df2)
  NameError: name 'df' is not defined
  2020-06-16 08_29_31.920157
  Program killed: running cleanup code
  </code></pre>