【发布时间】:2021-07-21 02:25:55
【问题描述】:
我正在将数据从 Postgres 移动到雪花。最初它有效,但我添加了:
df_postgres["dateutc"]= pd.to_datetime(df_postgres["dateutc"])
因为日期格式被错误地加载到雪花中,现在我看到了这个错误:
SQL 编译错误:位置 87 处的错误第 1 行无效标识符 '"dateutc"'
这是我的代码:
from sqlalchemy import create_engine
import pandas as pd
import glob
import os
from config import postgres_user, postgres_pass, host,port, postgres_db, snow_user, snow_pass,snow_account,snow_warehouse
from snowflake.connector.pandas_tools import pd_writer
from snowflake.sqlalchemy import URL
from sqlalchemy.dialects import registry
registry.register('snowflake', 'snowflake.sqlalchemy', 'dialect')
engine = create_engine(f'postgresql://{postgres_user}:{postgres_pass}@{host}:{port}/{postgres_db}')
conn = engine.connect()
#reads query
df_postgres = pd.read_sql("SELECT * FROM rok.my_table", conn)
#dropping these columns
drop_cols=['RPM', 'RPT']
df_postgres.drop(drop_cols, inplace=True, axis=1)
#changed columns to lowercase
df_postgres.columns = df_postgres.columns.str.lower()
df_postgres["dateutc"]= pd.to_datetime(df_postgres["dateutc"])
print(df_postgres.dateutc.dtype)
sf_conn = create_engine(URL(
account = snow_account,
user = snow_user,
password = snow_pass,
database = 'test',
schema = 'my_schema',
warehouse = 'test',
role = 'test',
))
df_postgres.to_sql(name='my_table',
index = False,
con = sf_conn,
if_exists = 'append',
chunksize = 300,
method = pd_writer)
【问题讨论】:
-
我有点确定您的表在某些时候是使用常规标识符创建的,即未引用。在这种情况下,雪花以大写形式存储它们:docs.snowflake.com/en/sql-reference/…。现在由于某种原因,
pd_writer被指示引用标识符(delimited),因此无法找到"dateutc"。那或表确实没有列,并且由于您使用'append'它失败了。尝试将其命名为DATEUTC看看会发生什么。 -
@IljaEverilä 感谢您的回复,我看到您提到了 pd_writer,我删除了它并且它起作用了!
标签: python sqlalchemy snowflake-cloud-data-platform