Skip to main content

A user friendly way to construct conditions for pandas dataframe query and sql

Project description

Condition

PyPI version PyPI Docs pipeline status coverage report

Project Description

This project provides a user friendly way to construct a condition object. The condition object can be passed around and later used in various contexts, such as querying pandas Dataframe, filtering pyarrow partitions or generating where clauses in SQL. It takes care of formating and syntax for you. It also supports extensibility to new usage contexts.

A Quick Example

>>> from condition import *
>>> import pandas as pd
>>> fl = FieldList('A B C date value'.split())
>>> cond = ((fl.date >= pd.to_datetime('20000101')) 
...             & (fl.date <= pd.to_datetime('20010131'))
...             & (fl.A == 'a1 a3'.split()) \
...             & (fl.B == 'b1')) \
...             | (fl.C != 'c3 c5'.split())
>>> print(cond) # show as sql where condition

        (
                (date >= '2000-01-01 00:00:00'
                and date <= '2001-01-31 00:00:00'
                and A in ('a1','a3')
                and B = 'b1')
        or C not in ('c3','c5'))

>>> cond.to_df_query()
"(((date >= '2000-01-01 00:00:00')&(date <= '2001-01-31 00:00:00')&(A in ('a1','a3'))&(B == 'b1'))|(C not in ('c3','c5')))"
>>> cond.to_pyarrow_filter()
[[('date', '>=', Timestamp('2000-01-01 00:00:00')),
  ('date', '<=', Timestamp('2001-01-31 00:00:00')),
  ('A', 'in', {'a1', 'a3'}),
  ('B', '=', 'b1')],
 [('C', 'not in', {'c3', 'c5'})]]

SQL Generation

>>> from condition.sql import render_sql
>>> sql = """
...     select *
...     from my_table
...     where {{where_condition}}
... """
>>> print(render_sql(sql, cond))

    select *
    from my_table
    where 
        (
                (date >= '2000-01-01 00:00:00'
                and date <= '2001-01-31 00:00:00'
                and A in ('a1','a3')
                and B = 'b1')
        or C not in ('c3','c5'))

Please see Usage Examples for detailed examples.

Installation

This project is distributed via pip. To get started:

pip install condition

To install jinja2 package used by condition.sql, do the following

pip install "condition[sql]"

To install all packages for development, do the following

pip install "condition[dev]"

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

condition-1.0.8.tar.gz (35.2 kB view details)

Uploaded Source

Built Distribution

condition-1.0.8-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file condition-1.0.8.tar.gz.

File metadata

  • Download URL: condition-1.0.8.tar.gz
  • Upload date:
  • Size: 35.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for condition-1.0.8.tar.gz
Algorithm Hash digest
SHA256 c5b96dd18622784777b4792e8dbc793e56d9d760039c29288b3c2c5706f0797c
MD5 66c0c944889537bb74aef2c2ba1c9977
BLAKE2b-256 cc86469af68ee68581c51d4a3cf00afb4697e23087814f672be8c9d89d217892

See more details on using hashes here.

File details

Details for the file condition-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: condition-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for condition-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 09f815d33b8a2b277f2afbb7aa341637e207a273b858e7dcc1c883c4030b7571
MD5 32e3d8877ad702b977ac054b790a4793
BLAKE2b-256 ec5cc124e4cb5e8ef3da5e7c35d5d09a4d8535362e1c64b91c70b580bb642aa2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page