将数据帧从Python插入到Snowflake Null问题

问题描述

这是我的代码片段-

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))

雪花上的表格如下所示:

数据帧如下所示:

这是我的data.info()GTC数据帧的外观:

<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

我不明白为什么会出现此错误:

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

我在我的数据帧中没有看到任何空值,并且仍然收到此错误。救命!


解决方案

问题似乎与";gtc name";中的空格字符有关。如果(在DataFrame和表上)使用GTC_NAME,它将起作用。

相关文章