dhruv gami
dhruv gami

Reputation: 57

Inserting dataframe from Python into Snowflake Null issue

Here is my code snippet -

import datetime
from snowflake import connector
from snowflake.connector.pandas_tools import write_pandas
import pandas as pd
import pickle


class SnowflakeDataFetcher:
    def __init__(self):
        self.username = '***'
        self.password = '***'
        self.ctx_a = connector.connect(user=self.username, password=self.password, account='***',
                                         warehouse='***', database='***', schema='***', role='***')

    def upload_data(self, mode, data, tablename, virt_date):
        if mode == 'test_table_upload':
            data.info()
            query_output = write_pandas(self.ctx_a, data, table_name=tablename)
            print("Success!")


if __name__ == "__main__":
    snowflake_data_fetcher = SnowflakeDataFetcher()
    with open(r'***', 'rb') as gtc_data_pickle:
        gtc_data = pickle.load(gtc_data_pickle)
        snowflake_data_fetcher.upload_data('test_table_upload', gtc_data, "TEST_DATA", datetime.date(2021, 3, 17))

This is what the table on snowflake looks like: enter image description here

This what the dataframe looks like:

enter image description here

This is what my data.info() looks like for my gtc dataframe:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10056 entries, 0 to 10055
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Datetime  10056 non-null  object
 1   Date      10056 non-null  object
 2   GTC Name  10056 non-null  object
dtypes: object(3)
memory usage: 235.8+ KB

I don't understand why I'm getting this error:

snowflake.connector.errors.ProgrammingError: 100072 (22000): NULL result in a non-nullable column

I don't see any null values in my dataframe and I'm still getting this error. Help!

Upvotes: 1

Views: 851

Answers (1)

Gokhan Atil
Gokhan Atil

Reputation: 10039

It seems the issue is about the space character in "GTC Name". If you use GTC_Name (on both your DataFrame and table), it will work.

Upvotes: 1

Related Questions