Pandas-SQLAlchemy integration

amancevice, updated 🕥 2023-01-12 14:57:38

RedPanda: Pandas & SQLAlchemy

pypi python pytest coverage maintainability

Two great tastes that taste great together.

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

Installation

bash pip install redpanda

Basic Use

Create a session from a SQLAlchemy engine:

```python import redpanda

engine = redpanda.create_engine("sqlite://")

=> Engine(sqlite://)

Session = redpanda.orm.sessionmaker(bind=engine) session = Session()

=>

```

Querying

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

```python query = session.query(MyModel)

=>

query.frame()

=>

```

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():

```python 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:

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

Releases

0.5.1 2021-11-02 02:42:10

Python 3.10 support

0.5.0 2021-03-16 14:08:27

Support for SQLAlchemy ~> 1.4 Support Python 3.7+

0.4.7 2020-12-26 17:16:02

Support for Python 3.9

0.4.6 2020-05-16 22:02:11

0.4.0 2019-04-16 20:52:33

Require SQLAlchemy >= 1.3.0

0.3.2 2018-08-26 15:34:14

Project setup updates

Alexander Mancevice

senior cloud infrastructrician

GitHub Repository

python pandas sqlalchemy