将数据帧写入雪花错误:不支持在类型(时间戳)中绑定数据

时间:2021-07-09 22:33:11

标签: python pandas snowflake-cloud-data-platform

我有一个 Pandas 的数据框,我正在尝试将其写回 Snowflake。但是,尝试这样做时,我收到以下错误:

(snowflake.connector.errors.ProgrammingError) 252004: Failed processing pyformat-parameters: 255001: Binding data in type (timestamp) is not supported.

我尝试使用 .dt.to_pydatetime() 转换我的日期列,但仍然没有运气。

这是我的代码的样子




#write data back into table
# Fill in your SFlake details here
engine = create_engine(URL(
    account = '######',
    user = 'mike',
    authenticator = 'externalbrowser',
    database = 'DatbaseName',
    schema = 'SchemaName',
    warehouse = 'WarehouseName',
    role='RoleName',
))
 
connection = engine.connect()


#FIX ATTEMPT: Binding data in type (timestamp) is not supported in Snowflake, here is the work around: 
df['month_end'] = df['month_end'].dt.to_pydatetime()
df['NEW_CUST_DT'] = df['NEW_CUST_DT'].dt.to_pydatetime()

df.to_sql('engagement', con=engine, index=False, if_exists='append') #make sure index is False, Snowflake doesnt accept indexes
 
connection.close()
engine.dispose()

供参考的数据框详细信息:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

Here 是可下载格式的代码。

我使用的是您可以从 yahoo api 获取的 csv 股票数据,因为您没有提供任何数据或代码来轻松重现您的问题。

month_end 和 NEW_CUST_DT 被添加到数据中以匹配您所做的转化。

我怀疑您的表格可能存在类型不匹配,或者您的源数据有问题。

尝试将数据发送到新表以查看是否有效。如果是这样,要么删除并重新创建您的表,要么修改您的数据框以匹配。您采用哪种策略取决于您的代码库的成熟程度和您的用例。

我的数据框中的类型是:

date           datetime64[ns]
open                  float64
high                  float64
low                   float64
close                 float64
adjclose              float64
volume                  int64
ticker                 object
month_end      datetime64[ns]
NEW_CUST_DT    datetime64[ns]

测试上传的代码,对我有用的是:

import pandas as pd
import sqlalchemy as sa
import pandas as pd
from sqlalchemy import *
import snowflake.connector


def savedfToSnowflake(df):
    engine = sa.create_engine(
        'snowflake://{user}:{password}@{account}/'.format(
            user=cred['u'],
            password=cred['p'],
            account=cred['a'],
        )
    )
    try:
        connection = engine.connect()
        print("Connected to Snowflake ")
        df.to_sql('stockprice', con=engine, index=False,
                  if_exists='append')  # make sure index is False, Snowflake doesnt accept indexes
        print("Successfully saved data to snowflake")
    except Exception as ex:
        print("Exception occurred {}".format(ex))
    finally:
        # close connection
        if connection:
            connection.close()
        if engine:
            engine.dispose()


def getData():
    df = pd.read_csv('/sampledata/AA.csv')
    df = df.tail(20)
    df['date'] = pd.to_datetime(df['date'])
    df['month_end'] = df['date'].dt.to_pydatetime()
    df['NEW_CUST_DT'] = df['date'].dt.to_pydatetime()
    print(df.dtypes)
    return df


def main():
    df = getData()
    savedfToSnowflake(df)


if __name__ == '__main__':
    main()

我的结果表是

create or replace TABLE ENGAGEMENT (
DATE TIMESTAMP_NTZ(9),
OPEN FLOAT,
HIGH FLOAT,
LOW FLOAT,
CLOSE FLOAT,
ADJCLOSE FLOAT,
VOLUME NUMBER(38,0),
TICKER VARCHAR(16777216),
MONTH_END TIMESTAMP_NTZ(9),
NEW_CUST_DT TIMESTAMP_NTZ(9));

验证结果为:

Show Results from Code