A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/developer-guide/python-connector/python-connector-pandas below:

Website Navigation


Using pandas DataFrames with the Python Connector

Using pandas DataFrames with the Python Connector

pandas is a library for data analysis. With pandas, you use a data structure called a DataFrame to analyze and manipulate two-dimensional data (such as data from a database table).

For more information see the pandas documentation.

If you need to get data from a Snowflake database to a pandas DataFrame, you can use the API methods provided with the Snowflake Connector for Python. The connector also provides API methods for writing data from a pandas DataFrame to a Snowflake database.

Note

Some of these API methods require a specific version of the PyArrow library. See Requirements for details.

For more information, see the PyArrow library documentation.

Requirements

Currently, the pandas-oriented API methods in the Python connector API work with:

Installation

To install the pandas-compatible version of the Snowflake Connector for Python, execute the command:

pip install "snowflake-connector-python[pandas]"

Copy

You must enter the square brackets ([ and ]) as shown in the command. The square brackets specify the extra part of the package to install.

For more information, see the extras Python dependency.

Use quotes around the name of the package (as shown) to prevent the square brackets from being interpreted as a wildcard.

If you need to install other extras (for example, secure-local-storage for caching connections with browser-based SSO or caching MFA tokens), use a comma between the extras:

pip install "snowflake-connector-python[secure-local-storage,pandas]"

Copy

Reading data from a Snowflake database to a pandas DataFrame

To read data into a pandas DataFrame, you use a Cursor to retrieve the data and then call one of these Cursor methods to put the data into a pandas DataFrame:

Writing data from a pandas DataFrame to a Snowflake database

To write data from a pandas DataFrame to a Snowflake database, do one of the following:

Snowflake to pandas data mapping

The table below shows the mapping from Snowflake data types to pandas data types:

Snowflake Data Type

pandas Data Type

FIXED NUMERIC type (scale = 0) except DECIMAL

(u)int{8,16,32,64} or float64 (for NULL)

FIXED NUMERIC type (scale > 0) except DECIMAL

float64

FIXED NUMERIC type DECIMAL

decimal

FLOAT/DOUBLE

float64

VARCHAR

str

BINARY

str

VARIANT

str

DATE

object (with datetime.date objects)

TIME

pandas.Timestamp(np.datetime64[ns])

TIMESTAMP_NTZ, TIMESTAMP_LTZ, TIMESTAMP_TZ

pandas.Timestamp(np.datetime64[ns])

Notes:

Importing pandas

Customarily, pandas is imported with the following statement:

You might see references to pandas objects as either pandas.object or pd.object.

Migrating to pandas DataFrames

This section is primarily for users who have used pandas (and possibly SQLAlchemy) previously.

Previous pandas users might have code similar to either of the following:

Code that is similar to either of the preceding examples can be converted to use the Python connector pandas API calls listed in Reading Data from a Snowflake Database to a pandas DataFrame (in this topic).

Note

With support for pandas in the Python connector, SQLAlchemy is no longer needed to convert data in a cursor into a DataFrame.

However, you can continue to use SQLAlchemy if you wish; the Python connector maintains compatibility with SQLAlchemy.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4