Pandas-SQLAlchemy integration
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
notebooks Fixes for SQLAlchemy 1.1 Aug 31, 2017
redpanda Added pytest.ini Oct 19, 2017
tests Fixes for SQLAlchemy 1.1 Aug 31, 2017
.gitignore Added Pipfile Apr 6, 2018
.travis.yml project setup Aug 26, 2018
LICENSE Initial commit Oct 3, 2015
Pipfile Added Pipfile Apr 6, 2018
Pipfile.lock Added Pipfile Apr 6, 2018
README.md examples -> notebooks Aug 13, 2016
docker-compose.yml examples -> notebooks Aug 13, 2016
pytest.ini Added pytest.ini Oct 19, 2017
setup.py project setup Aug 26, 2018
tox.ini project setup Aug 26, 2018

README.md

RedPanda: Pandas & SQLAlchemy

build codecov pypi python

Two great tastes that taste great together.

Use RedPanda to add simple pandas integration into your declarative models.

Installation

pip install redpanda

Basic Use

Create a session from a SQLAlchemy engine:

import redpanda

engine = redpanda.create_engine("sqlite://")
# => Engine(sqlite://)

Session = redpanda.orm.sessionmaker(bind=engine)
session = Session()
# => <sqlalchemy.orm.session.Session>

Querying

Use the frame() method of RedPanda queries to return a DataFrame representation of the results instead of a collection of models.

query = session.query(MyModel)
# => <redpanda.orm.Query>

query.frame()
# => <pandas.DataFrame>

Querying with Filters

The frame() method that wraps the pandas.read_sql() function into a dialect-agnostic class-method for declarative SQLAlchemy models and can accept the same keyword arguments as pandas.read_sql():

query = session.query(MyModel).filter_by(my_attr="my_val")

query.frame(index_col="time")

Additionally, a within() method is added to SQLAlchemy's InstrumentedAttribute class that accepts a pandas Index object:

index = pandas.period_range("2016-11-01", "2016-11-30", freq="W")
query = session.query(MyModel).filter(MyModel.timestamp.within(index))

More Examples

See the IPython Notebooks in the notebooks directory for examples using Python 2.7 and Python 3.5

Additionally, if you have docker-compose installed you may view these notebooks directly by cloning this repo and starting the containers:

git clone git@github.com:amancevice/redpanda.git
cd redpanda
docker-compose up

Navigate to http://localhost:2700 to view the Python 2.7 notebook, or http://localhost:3500 for Python 3.5.