title: How to use psycopg2.extras.execute_values
body: <p>I have a DF like this</p>
  <p>where I have converted <em><strong>expiry</strong></em> column from string to date time as</p>
  <blockquote>
  <p>df[&quot;expiry&quot;] = pd.to_datetime(df[&quot;expiry&quot;])</p>
  </blockquote>
  <pre><code>  instrument_token  exchange_token  tradingsymbol       name        expiry              strike  lot_size    instrument_type segment     exchange
  0  13873410         54193           AARTIIND21JUL800CE  AARTIIND    2021-07-29 00:00:00 800     850         CE              NFO-OPT         NFO
  0  13873666         54194           AARTIIND21JUL800PE  AARTIIND    2021-07-29 00:00:00 800     850         PE              NFO-OPT         NFO
  0  13873922         54195           AARTIIND21JUL810CE  AARTIIND    2021-07-29 00:00:00 810     850         CE              NFO-OPT         NFO
  0  13874178         54196           AARTIIND21JUL810PE  AARTIIND    2021-07-29 00:00:00 810     850         PE              NFO-OPT         NFO
  0  13874434         54197           AARTIIND21JUL820CE  AARTIIND    2021-07-29 00:00:00 820     850         CE              NFO-OPT         NFO
  </code></pre>
  <p>and I am getting the error</p>
  <pre><code>invalid input syntax for type timestamp: &quot;NaT&quot;
  LINE 1: ...aN'::float,'NaN'::float,'NaN'::float,'NaN'::float,'NaT'::tim...
  </code></pre>
  <p>I am not sure what I am doing wrong here.</p>
  <p>code snippet is given below</p>
  <pre><code>import psycopg2.extras as extras
  
  def execute_values(conn, df, table):
      # Create a list of tupples from the dataframe values
      tuples = [tuple(x) for x in df.to_numpy()]
      # Comma-separated dataframe columns
      cols = ','.join(list(df.columns))
      cursor = conn.cursor()
      #create a temp table to handle primary key conflict
      command= '''CREATE TEMPORARY TABLE IF NOT EXISTS _symbol (
          instrument_token INTEGER NOT NULL, exchange_token INTEGER, tradingsymbol TEXT NOT NULL, name TEXT,
          expiry TIMESTAMP, strike INTEGER, lot_size INTEGER,instrument_type TEXT, segment TEXT NOT NULL, exchange TEXT, PRIMARY KEY (instrument_token,
          tradingsymbol));'''
      query_main = '''INSERT INTO {table}(instrument_token, exchange_token, tradingsymbol, name, expiry, strike, lot_size,instrument_type, segment, exchange )
                  SELECT * FROM _symbol
                  ON CONFLICT (instrument_token, tradingsymbol)
                  DO NOTHING
              '''.format(table=table)
  
      try:
          cursor.execute(command)
          query_temp = &quot;INSERT INTO %s(%s) VALUES %%s &quot; % ('_symbol', cols)
          extras.execute_values(cursor, query_temp, tuples)
          cursor.execute(query_main)
          conn.commit()
      except (Exception, psycopg2.DatabaseError) as error:
          print(&quot;Error: %s&quot; % error)
          conn.rollback()
          cursor.close()
      else:
          cursor.close()
      if conn is not None:
          conn.close()
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/houWM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/houWM.png" alt="enter image description here" /></a></p>
  <p>data inside tuple while debugging is also timestamp :(</p>
  <p><a href="https://i.stack.imgur.com/MKwMv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MKwMv.png" alt="enter image description here" /></a></p>