MCPcopy Create free account
hub / github.com/feast-dev/feast / write_pandas

Function write_pandas

sdk/python/feast/infra/utils/snowflake/snowflake_utils.py:185–252  ·  view source on GitHub ↗

Allows users to most efficiently write back a pandas DataFrame to Snowflake. It works by dumping the DataFrame into Parquet files, uploading them and finally copying their data into the table. Returns whether all files were ingested correctly, number of chunks uploaded, and number of rows

(
    conn: SnowflakeConnection,
    df: pd.DataFrame,
    table_name: str,
    database: Optional[str] = None,
    schema: Optional[str] = None,
    chunk_size: Optional[int] = None,
    compression: str = "gzip",
    on_error: str = "abort_statement",
    parallel: int = 4,
    quote_identifiers: bool = True,
    auto_create_table: bool = False,
    create_temp_table: bool = False,
)

Source from the content-addressed store, hash-verified

183# Remove dependency on write_pandas function by falling back to native snowflake python connector
184# Current issue is datetime[ns] types are read incorrectly in Snowflake, need to coerce to datetime[ns, UTC]
185def write_pandas(
186 conn: SnowflakeConnection,
187 df: pd.DataFrame,
188 table_name: str,
189 database: Optional[str] = None,
190 schema: Optional[str] = None,
191 chunk_size: Optional[int] = None,
192 compression: str = "gzip",
193 on_error: str = "abort_statement",
194 parallel: int = 4,
195 quote_identifiers: bool = True,
196 auto_create_table: bool = False,
197 create_temp_table: bool = False,
198):
199 """Allows users to most efficiently write back a pandas DataFrame to Snowflake.
200
201 It works by dumping the DataFrame into Parquet files, uploading them and finally copying their data into the table.
202
203 Returns whether all files were ingested correctly, number of chunks uploaded, and number of rows ingested
204 with all of the COPY INTO command's output for debugging purposes.
205
206 Example usage:
207 import pandas
208 from snowflake.connector.pandas_tools import write_pandas
209
210 df = pandas.DataFrame([('Mark', 10), ('Luke', 20)], columns=['name', 'balance'])
211 success, nchunks, nrows, _ = write_pandas(cnx, df, 'customers')
212
213 Args:
214 conn: Connection to be used to communicate with Snowflake.
215 df: Dataframe we'd like to write back.
216 table_name: Table name where we want to insert into.
217 database: Database table is in, if not provided the connection one will be used.
218 schema: Schema table is in, if not provided the connection one will be used.
219 chunk_size: Number of elements to be inserted once, if not provided all elements will be dumped once
220 (Default value = None).
221 compression: The compression used on the Parquet files, can only be gzip, or snappy. Gzip gives supposedly a
222 better compression, while snappy is faster. Use whichever is more appropriate (Default value = 'gzip').
223 on_error: Action to take when COPY INTO statements fail, default follows documentation at:
224 https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#copy-options-copyoptions
225 (Default value = 'abort_statement').
226 parallel: Number of threads to be used when uploading chunks, default follows documentation at:
227 https://docs.snowflake.com/en/sql-reference/sql/put.html#optional-parameters (Default value = 4).
228 quote_identifiers: By default, identifiers, specifically database, schema, table and column names
229 (from df.columns) will be quoted. If set to False, identifiers are passed on to Snowflake without quoting.
230 I.e. identifiers will be coerced to uppercase by Snowflake. (Default value = True)
231 auto_create_table: When true, will automatically create a table with corresponding columns for each column in
232 the passed in DataFrame. The table will not be created if it already exists
233 create_temp_table: Will make the auto-created table as a temporary table
234 """
235
236 cursor: SnowflakeCursor = conn.cursor()
237 stage_name = create_temporary_sfc_stage(cursor)
238
239 upload_df(df, cursor, stage_name, chunk_size, parallel, compression)
240 copy_uploaded_data_to_table(
241 cursor,
242 stage_name,

Callers 6

write_logged_featuresMethod · 0.90
offline_write_batchMethod · 0.90
to_snowflakeMethod · 0.90
_upload_entity_dfFunction · 0.90
bootstrapFunction · 0.90
create_data_sourceMethod · 0.90

Calls 3

upload_dfFunction · 0.85

Tested by

no test coverage detected