Module for quick and easy access to redshift using pandas
Project description
pandashift
Overview
Pandashift integrates Pandas with Amazon Redshift for smooth ETL processes and data manipulation. Specifically designed for Redshift in Python, it simplifies workflows by providing easy bulk inserts and automatically handling type conversions, including the ability to convert Pandas types to Redshift types. Data can be pulled from Redshift into Pandas using the function read_query for quick analysis and data processing in Python. Whether creating DataFrames from Redshift or performing ETL tasks, taking advantage of Pandas and Pandashift provides flexibility and efficiency for scalable data workflows.
Installing
pip install pandashift
Usage
There are 2 ways work with this package
- Setting up environment variables
- Passing credentials on every call
Below are examples of both approaches
With environment variables set up
from pandashift import read_query, execute_query, load_db, create_table_from_df
# Read Data
df = read_query('SELECT * FROM public.test')
# Execute statement (aka table create/drop/etc)
execute_query('DROP public.test')
# Create the table from df
create_table_from_df(df, 'public.test')
# Loading dataframe
load_df(df, table_name ='public.test')
No environment variables
from pandashift import read_query, execute_query, load_db, create_table_from_df
creds = {
"host":"YOUR HOST",
"port":"YOUR PORT",
"dbname":"YOUR DATABASE",
"user":"YOUR USER",
"password":"YOUR PASSWORD"
}
# Read Data
df = read_query('SELECT * FROM public.test',credentials = creds)
# Execute statement (aka table create/drop/etc)
execute_query('DROP public.test',credentials = creds)
# Create the table from df
create_table_from_df(df, 'public.test', credentials = creds)
# Loading dataframe
load_df(df, table_name = 'public.test',credentials = creds)
Functions
load_df
| Parameter | Usage |
|---|---|
| init_df | The dataframe for loading |
| table_name | The table that you want to load the df to |
| credentials | Credentials to use for connection if any |
| auto_create_table | Create the table if it doesn't exist, by default False |
| verify_column_names | The checks that the dataframe column order matches the table column order, by default |
| empty_str_as_null | This option will interpret empty string '' as NULL, by default True |
| maximum_insert_length | Maximum length of the insert statement, alter this if you get error exceeding the max statement length, by default 16000000 |
| perform_analyze | If this is true at the end of loading will run ANALYZE table, by default False |
create_table_from_df
Parameter |
Usage |
|---|---|
| df | The dataframe for loading |
| table_name | The table that you want to load the df to |
| credentials | Credentials to use for connection if any |
| sortkeys | Specify desired sortkeys, by default no sortkeys are used |
| distkey | Specify desired sortkeys, by default no distkeys are used |
| threshold | Specifies the threshold for datatype conversion (eg if more than 80% of values are float will create FLOAT column ). Must be a value between 0 and 1, by default 0.8 |
| show_ddl | Prints the DDL of the created table, by default False |
| no_execute | Just creates DDL, but doesn't run it in database (useful for debugging), by default False |
Currently only the these datatypes are suppourted:
- SMALLINT
- INTEGER
- BIGINT
- DECIMAL
- REAL
- DOUBLE
- TIMESTAMP
- DATE
- CHAR
- VARCHAR
- BOOLEAN
Testing
In order to run local testing
- Create an environment with required modules
pip install -r requirements.txt
- Run the following
pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pandashift-1.0.4.tar.gz.
File metadata
- Download URL: pandashift-1.0.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb92e2b94e1fb76a22eddc4c96f1ea5ebd0df61fe416cc676772e3de7a337606
|
|
| MD5 |
c79d296b03700da778e058a7e75b4f11
|
|
| BLAKE2b-256 |
651abcd74975a09225f5e8133bc6fdfdbae70b950e1c3851dee349dba7ff6747
|
File details
Details for the file pandashift-1.0.4-py3-none-any.whl.
File metadata
- Download URL: pandashift-1.0.4-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3262e13fb5e2f77273dbe9ec2b87e16f3fc6b9f9a81fdaca075fc7490203d677
|
|
| MD5 |
9dcb2ca78d45c3a25326069ccb0f6875
|
|
| BLAKE2b-256 |
b6cbf0118ab76f7e261d2def64598def8bdfc631e0a1bb9324154aedbe95dab1
|