title: Invalid Date when inserting to Teradata using Python
body: <p>I'm working on a python piece that will insert a dataframe into a teradata table using pyodbc. The error I can't get past is...</p>
  
  <pre><code>File "file.py", line 33, in &lt;module&gt;
  cursor.execute("INSERT INTO DB.TABLE (MASDIV,TRXTYPE,STATION,TUNING_EVNT_START_DT,DOW,MOY,TRANSACTIONS)VALUESrow['MASDIV'],'trx_chtr',row['STATION'],row['TUNING_EVNT_START_DT'],row['DOW'],row['MOY'],row['TRANSACTIONS'])
  pyodbc.DataError: ('22008', '[22008] [Teradata][ODBC Teradata Driver][TeradataDatabase] Invalid date supplied for Table.TUNING_EVNT_START_DT. (-2666) (SQLExecDirectW)')
  </code></pre>
  
  <p>To fill you in... I've got a Teradata table that I want to take a dataframe and insert it into. That table is made as.</p>
  
  <pre><code>CREATE SET TABLE  DB.TABLE, FALLBACK
     (PK decimal(10,0) NOT NULL GENERATED ALWAYS AS IDENTITY
              (START WITH 1 
              INCREMENT BY 1 
              MINVALUE 1 
              --MAXVALUE 2147483647 
              NO CYCLE),
      TRXTYPE VARCHAR(10),
      MASDIV VARCHAR(30),
      STATION VARCHAR(50),
      TUNING_EVNT_START_DT DATE format 'MM/DD/YYYY',
      DOW VARCHAR(3),
      MOY VARCHAR(10),
      TRANSACTIONS INT,
      ANOMALY_FLAG INT NOT NULL DEFAULT 1)
  PRIMARY INDEX (PK);
  </code></pre>
  
  <p>The primary key and anomaly_flag will be automatically filled in. Below is the script that I am using and running into the error. It is reading in a csv and creating a dataframe. The first two lines (including a header) of the csv look like...</p>
  
  <pre><code>MASDIV              | STATION                    | TUNING_EVNT_START_DT | DOW |    MOY    | TRANSACTIONS
  
  Staten Island       | WFUTDT4                    |         9/12/18      | Wed | September | 538
  
  San Fernando Valley | American Heroes Channel HD |        6/28/2018     | Thu | June      | 12382
  </code></pre>
  
  <p>Here is the script that I am using...</p>
  
  <pre><code> '''
  Written by Bobby October 1st, 2018
  REFERENCE
  https://tomaztsql.wordpkress.com/2018/07/15/using-python-pandas-dataframe-to-read-and-insert-data-to-microsoft-sql-server/
  '''
  
  import pandas as pd
  import pyodbc
  from datetime import datetime
  
  #READ IN CSV TEST DATA
  df = pd.read_csv('Data\\test_set.csv')
  print('CSV LOADED')
  
  #ADJUST DATE FORMAT
  df['TUNING_EVNT_START_DT'] = pd.to_datetime(df.TUNING_EVNT_START_DT)
  #df['TUNING_EVNT_START_DT'] = 
  df['TUNING_EVNT_START_DT'].dt.strftime('%m/%d/%Y')
  df['TUNING_EVNT_START_DT'] = df['TUNING_EVNT_START_DT'].dt.strftime('%Y-%m-%d')
  print('DATE FORMAT CHANGED')
  print(df)
  
  #PUSH TO DATABASE
  conn = pyodbc.connect('dsn=ConnectR')
  cursor = conn.cursor()
  
  # Database table has columns...
  # PK | TRXYPE | MASDIV | STATION | TUNING_EVNT_START_DT | DOW | MOY | 
  TRANSACTIONS | ANOMALY_FLAG
  # PK is autoincrementing, TRXTYPE needs to be specified on insert command, 
  and ANOMALY_FLAG defaults to 1 for yes
  
  for index, row in df.iterrows():
          cursor.execute("INSERT INTO DLABBUAnalytics_Lab.Anomaly_Detection_SuperSet(MASDIV,TRXTYPE,STATION,TUNING_EVNT_START_DT,DOW,MOY,TRANSACTIONS)VALUES(?,?,?,?,?,?,?)", row['MASDIV'],'trx_chtr',row['STATION'],row['TUNING_EVNT_START_DT'],row['DOW'],row['MOY'],row['TRANSACTIONS'])
      conn.commit()
      print('RECORD ENTERED')
  
  print('DF SUCCESSFULLY WRITTEN TO DB')
  
  #PULL FROM DATABASE
  sql_conn = pyodbc.connect('dsn=ConnectR')
  query = 'SELECT * FROM DLABBUAnalytics_Lab.Anomaly_Detection_SuperSet;'
  df = pd.read_sql(query, sql_conn)
  print(df)
  </code></pre>
  
  <p>So in this I am converting the date format and trying to insert row by row into the Teradata table. The first record reads in and is in the database. The second record throws the error that is at the top. The date is 6/28/18 and I've changed it to 6/11/18 just to see if there was a mix up with day and month, but that still had the same problem. Are the columns getting off somewhere and it is trying to insert a different column's value into the date column.</p>
  
  <p>Any ideas or help is much appreciated!</p>