Skip to main content

pandaq: An easy pandas query-string builder

test codecov PyPI - Version PyPI - Python Version

This library provides q method for easy querying of pandas.DataFrame. Internally, q generates the query string for pandas.DataFrame.query.

The goal of pandaq is to save time when querying.

Installation

pip install pandaq

Usage

pandaq provides two ways to select pandas.DataFrame rows by query string.

A. Generate a query-string

from pandaq import Q
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv')
qstr = Q().q(PassengerId=1)  # -> "PassengerId==1"
df.query(qstr)

B. Add q method to pandas.DataFrame

import pandaq.patch
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv')
df.q(PassengerId=1)

How to construct a query

Basics

Query Type How It Works
q("str") Equivalent to pd.DataFrame.query("str")
q(dict) Works like q(**dict)
q(k=v) Where the column named k is equal to v
q(k=[v1, ...]) Where the column named k is in [v1, ...]
q(k=(op1, v1, ...)) Where the column named k meets the condition k op1 v1 and ...
q(k="!str") Where the column named k is NOT str (see Advance)
q(k="?str") Where the column named k contains str (see Advance)
q(k="!?str") Where the column named k does NOT contain str (see Advance)
q(k="/str") Where the column named k match the regex str (see Advance)

Example

Type Kind pandaq.q Equivalent to query-string
Number Equal q(a=1) "a==1"
Number Contain q(a=[1, 2]) "a==1 or a==2"
Number Inequality q(a=(">", 1)) "a>1"
Number Inequality q(a=(">", 1, "<=", 3)) "a>1 and a<=3"
Bool Equal q(a=True) "a==True"
Str Full-Match q(a="text") 'a=="text"'
Str Not Full-Match q(a="!text") 'a!="text"'
Str Partial-Match q(a="?text") 'a.str.contains("text", regex=False, na=False)'
Str Not Partial-Match q(a="!?text") 'not a.str.contains("text", regex=False, na=False)'
Str Regex-Match q(a="/textA|textB") 'a.str.contains("textA|textB", regex=True, na=False)'
Datetime Equal q(a=dt.date(1970,1,1)) 'a=="1970-01-01"'
Datetime Inequality q(a=(">", dt.date(1970,1,1))) 'a>"1970-01-01"'

Combination

pandaq.q Equivalent to query-string
q(a=1, b=1) "(a==1 & b==1)"
q(a=1).q(b=1) "a==1 & b==1"

Advanced Usage

To set the prefix individually, configure the following settings.

from pandaq import config

# Default settings
config.str_prefix = StringPrefix(
    neq="!",  # The prefix for not equal
    regex="/",  # The prefix for regular expressions
    partial="?",  # The prefix for partial match
    neq_partial="!?",  # The prefix for not equal partial match
)

License

pandaq is distributed under the terms of the MIT license.

Metadata

Release files for pandaq 0.1.0

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

Source distribution (sdist)

Source distribution for pandaq 0.1.0
File Size Uploaded Provenance Actions
pandaq-0.1.0.tar.gz 11.0 kB Without provenance Details for pandaq-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pandaq 0.1.0
File Interpreter ABI Platform Actions
pandaq-0.1.0-py3-none-any.whl Python 3 none any Details for pandaq-0.1.0-py3-none-any.whl

Total release size: 21.1 kB

Release files / pandaq-0.1.0.tar.gz

Download URL pandaq-0.1.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
22850e6f5a21f0a55c710257f0989f0b7e2ab7842785afc98660499552ea44bd
BLAKE2b-256 checksum
How to use checksums
9be2b8dd5bd47c3f5de5ef3ce0e71bb468506850d55e5cf583bd0a29e91a9f46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / pandaq-0.1.0-py3-none-any.whl

Download URL pandaq-0.1.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b134a3674b0cdd8bccb50aa9f1d3827ce3284b17dd1bd5e07e9a53268f82b882
BLAKE2b-256 checksum
How to use checksums
3824d34f148f9c373799438062697014ed03f0a7f02886880650d1d872625500
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Security for pandaq 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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