{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Data Science Library Integrations" ] }, { "cell_type": "markdown", "source": [ "# Overview\n", "`redshift_connector` provides integration with [pandas](https://github.com/pandas-dev/pandas) and [numpy](https://github.com/numpy/numpy) to enable everyone who works with data a simple interface for reading and writing data to and from Amazon Redshift." ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Retrieving data as a `pandas.DataFrame`\n", "`redshift_connector`'s `fetch_dataframe()` method allows users to directly retrieve result sets as a `pandas.DataFrame`. An example workflow using this function is shown below" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import pandas\n", "import redshift_connector\n", "with redshift_connector.connect(\n", " host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n", " database='dev',\n", " user='awsuser',\n", " password='my_password'\n", ") as conn:\n", " with conn.cursor() as cursor:\n", " cursor.execute(\"create Temp table book(bookname varchar,author‎ varchar)\")\n", " cursor.executemany(\"insert into book (bookname, author‎) values (%s, %s)\",\n", " [\n", " ('One Hundred Years of Solitude', 'Gabriel García Márquez'),\n", " ('A Brief History of Time', 'Stephen Hawking')\n", "\n", " ])\n", " cursor.execute(\"select * from book\")\n", " result: pandas.DataFrame = cursor.fetch_dataframe()\n", " print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Retrieving data as a `numpy.ndarray`\n", "`redshift_connector`'s `fetch_numpy_array()` method allows users to directly retrieve result sets as a `numpy.ndarray`. An example workflow using this function is shown below" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import numpy\n", "import redshift_connector\n", "with redshift_connector.connect(\n", " host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n", " database='dev',\n", " user='awsuser',\n", " password='my_password'\n", ") as conn:\n", " with conn.cursor() as cursor:\n", " cursor.execute(\"create Temp table book(bookname varchar,author‎ varchar)\")\n", " cursor.executemany(\"insert into book (bookname, author‎) values (%s, %s)\",\n", " [\n", " ('One Hundred Years of Solitude', 'Gabriel García Márquez'),\n", " ('A Brief History of Time', 'Stephen Hawking')\n", "\n", " ])\n", " cursor.execute(\"select * from book\")\n", " result: numpy.ndarray = cursor.fetch_numpy_array()\n", " print(result)\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Inserting a `pandas.DataFrame` into an Amazon Redshift database table\n", "`redshift_connector`'s `write_dataframe()` method allows users to directly insert a`pandas.DataFrame` to a database table. An example workflow using this function is shown below" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import redshift_connector\n", "\n", "df: pd.DataFrame = pd.DataFrame(\n", " np.array(\n", " [\n", " [\"One Hundred Years of Solitude\", \"Gabriel García Márquez\"],\n", " [\"A Brief History of Time\", \"Stephen Hawking\"],\n", " ]\n", " ),\n", " columns=[\"bookname\", \"author‎\"],\n", ")\n", "\n", "with redshift_connector.connect(\n", " host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n", " database='dev',\n", " user='awsuser',\n", " password='my_password'\n", ") as conn:\n", " with conn.cursor() as cursor:\n", " cursor.execute(\"create Temp table book(bookname varchar,author‎ varchar)\")\n", " cursor.write_dataframe(df, \"book\")\n", " cursor.execute(\"select * from book;\")\n", " result = cursor.fetchall()\n", " print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }