A user friendly way to construct conditions for pandas dataframe query and sql
Project description
Condition
Project Description
This project provides a user friendly way to construct a condition object. The condition object can later used to query pandas Dataframe, filter pyarrow partitions or to generate where conditions in SQL. It takes care of formating and syntax for you.
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())
>>> 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
condition-1.0.3-py3-none-any.whl
(17.2 kB
view hashes)
Close
Hashes for condition-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 062cc02ccd82d0a4ae41ba1aab6c6fa4292986fd4505eebab5c22a5afc64cdb5 |
|
MD5 | e033f8005093a596aa21ad3c5d4e7138 |
|
BLAKE2b-256 | 7e1052ff5f480c628cc1e7b6d86528198ca0f5acdb2171456c2ca2cc94b93acd |