Home > Enterprise >  Write pandas.DataFrame to snowflake with snowflake.snowpark
Write pandas.DataFrame to snowflake with snowflake.snowpark

Time:11-17

I need write dataframe to snowflake, by using snowflake.snowpark. I have some pandas.DataFrame, have some transformation (corr matrix, description stats, model output,...), but I cant write it back to my snowflake database. Thanks for help.

from snowflake.snowpark import Session
from snowflake.snowpark import table
from snowflake.snowpark.functions import udf
from snowflake.snowpark.functions import col
from snowflake.snowpark.types import StringType

sess = None

print('Connecting...')

cnn_params = {
"account": "eu-west-1",
"user": "user",
"password": 'pass',
"warehouse": "xs",
"database": "demodb",
"schema": "demosch",
"role": "ACCOUNTADMIN"
}

try:
    print('session...')
    import pandas as pd
    import numpy as np
    sess = Session.builder.configs(cnn_params).create()
    
    df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
    #print(df.describe())
    
    df.describe().write.mode("overwrite").save_as_table("describe_output", table_type="temporary")
    
except Exception as e:
    print(e)
finally:
    if sess:
        sess.close()
        print('connection closed...')
print('done.')

My output:

'DataFrame' object has no attribute 'write'
connection closed...

CodePudding user response:

You have created a Pandas dataframe. You need a Snowflake dataframe to write back to the database. See example 3 in the Snowflake documentation for create a Snowflake dataframe

CodePudding user response:

You can use session.write_pandas(df) to write the pandas dataframe to a Snowflake table, or you can create a Snowpark dataframe using create_dataframe and then use .write.mode...

  • Related