Skip to main content
https://github.com/nens/condenser/workflows/Linux/badge.svg https://img.shields.io/pypi/v/condenser.svg

A fast interface between SQLAlchemy and Numpy

Features

This project reads data from SQLAlchemy into structured numpy arrays. Next to the builtin SQLAlchemy types, it also supports reading geometries into pygeos arrays of geometries.

Installation

Install via pip using:

$ pip install condenser

Include geometry support:

$ pip install condenser[geo]

Usage

condenser exposes one object: NumpyQuery, which is used as a custom SQLAlchemy query class as follows:

>>> from condenser import NumpyQuery
>>> # create the session with a custom query class
>>> session = session_factory(query_cls=NumpyQuery)

If a session is constructed like that, every query object will have an additional method to dump the selected data into a numpy structured array:

>>> query = session.query(SomeModel.float_type_column, SomeModel.int_type_column)
>>> my_array = query.as_structarray()

Geometry support

Geometry columns are automatically converted to arrays of pygeos.Geometry objects. See https://pygeos.readthedocs.io on for (vectorized) numpy functions that can act on these arrays.

Transform geometries (using the ST_Transform database function) as follows:

>>> query.with_transformed_geometries(target_srid=28992)

Note that this will only transform geometries with a known SRID. If an SRID is known only from another metadata source, use a function appropriate to your database backend to set the projection before converting it. Another option is using the pyproj library in combination with pygeos.apply to transform geometries from Python.

Custom dtype mapping

condenser has a safe approach on guessing the Numpy dtypes from SQLAlchemy dtypes. It always takes 8-byte signed integers and floats. For some database backends this can be changed to for example 4-byte datatypes. Override an SQLAlchemy to NumPy type mapping after constructing the query:

>>> from sqlalchemy import Integer
>>> query = session.query(SomeModel.float_type_column)
>>> query.numpy_settings[Integer]["dtype"] = np.int32
>>> query.as_structarray()

Or globally:

>>> NumpyQuery.default_numpy_settings[Integer]["dtype"] = np.int32

NULL values

Most numpy datatypes handle NULL (Python: None) values natively. Only integer typed columns deserve extra attention as they have no equivalent in the NumPy integer dtype. This package converts NULLs in integer columns to -1 by default. A complete list of what to expect:

  • NULL in integer columns becomes -1

  • NULL in float and numeric columns becomes nan

  • NULL in boolean columns becomes False

  • NULL in object typed columns (string, text, geometry) becomes None

Adjust the NULL value before executing the query:

>>> from sqlalchemy import Integer
>>> query = session.query(SomeModel.float_type_column)
>>> query.numpy_settings[Integer]["null"] = -9999
>>> query.numpy_settings[Boolean]["null"] = True
>>> query.as_structarray()

Or globally:

>>> NumpyQuery.default_numpy_settings[Integer]["null"] = -9999

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Release files for condenser 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for condenser 0.2.1
File Size Uploaded
condenser-0.2.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for condenser 0.2.1
File Interpreter ABI Platform
condenser-0.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.3 kB

Release files / condenser-0.2.1.tar.gz

Download URL condenser-0.2.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e416c1e81c85f8e4481cd7eb9e9a50d3a842672300e07f14a6ac9f2916e1b848
BLAKE2b-256 checksum
How to use checksums
7fa35885e009d94435747994342c5bb4436c00c9784d056401ef2022e67f0d46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / condenser-0.2.1-py2.py3-none-any.whl

Download URL condenser-0.2.1-py2.py3-none-any.whl
Size 6.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fafeea9a6d589db78b8cc3e6dd65a126d250b35afeed744fddf27ee857bc60b8
BLAKE2b-256 checksum
How to use checksums
ed6c2380101f86d77bd64b08f944dbce565c9d2457c6f8378a7061ef8158b0ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page