Skip to main content

Expand grid functionality using Polars

Project description

Polarsgrid

Continuous integration PyPI - Python Version

Tidyverse-style expand_grid() in a fast and efficient way using Polars.

Installation

Install the package as follows:

pip install polarsgrid

or add it to your project using uv as follows:

uv add polarsgrid

Usage

This package contains only a single function: expand_grid().

from polarsgrid import expand_grid
expand_grid(a=[1, 2], b=["x", "y"])
shape: (4, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪═════╡
│ 1   ┆ x   │
│ 2   ┆ x   │
│ 1   ┆ y   │
│ 2   ┆ y   │
└─────┴─────┘

Note that the first argument iteratest fastest, and the last argument iterates slowest in the cartesian product.

Should the grid become too big to hold in memory, you can choose to return a lazy table which can be stored on a disk efficiently:

lgrid = expand_grid(
    sample_size=list(range(1000)), 
    condition=["a", "b", "c"], 
    iteration=list(range(500)), 
    _lazy=True,
)
lgrid.sink_parquet("grid.parquet")

Additionally, for certain operations it might be nice to return the string columns as a categorical data type, and to add a row id:

expand_grid(
    fruit=["apple", "banana", "pear"], 
    color=["red", "green", "blue"], 
    check=[True, False], 
    _categorical=True,
    _row_id=True,
)
shape: (18, 4)
┌────────┬────────┬───────┬───────┐
│ row_id ┆ fruit  ┆ color ┆ check │
│ ---    ┆ ---    ┆ ---   ┆ ---   │
│ i64    ┆ cat    ┆ cat   ┆ bool  │
╞════════╪════════╪═══════╪═══════╡
│ 0      ┆ apple  ┆ red   ┆ true  │
│ 1      ┆ banana ┆ red   ┆ true  │
│ 2      ┆ pear   ┆ red   ┆ true  │
│ 3      ┆ apple  ┆ green ┆ true  │
│ 4      ┆ banana ┆ green ┆ true  │
│ …      ┆ …      ┆ …     ┆ …     │
│ 13     ┆ banana ┆ green ┆ false │
│ 14     ┆ pear   ┆ green ┆ false │
│ 15     ┆ apple  ┆ blue  ┆ false │
│ 16     ┆ banana ┆ blue  ┆ false │
│ 17     ┆ pear   ┆ blue  ┆ false │
└────────┴────────┴───────┴───────┘

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

polarsgrid-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polarsgrid-0.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file polarsgrid-0.2.0.tar.gz.

File metadata

  • Download URL: polarsgrid-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for polarsgrid-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fa3320db6e0b18849762d2a9c265a5e42532697305591204f0f6e287d03c6797
MD5 68c831ba5b5863136f505179d5090994
BLAKE2b-256 1861f3276c27d7c6043852da7a7ba87d29d02b904f93b2e53276df03b36febca

See more details on using hashes here.

Provenance

The following attestation bundles were made for polarsgrid-0.2.0.tar.gz:

Publisher: publish.yml on vankesteren/polarsgrid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polarsgrid-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: polarsgrid-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for polarsgrid-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78157b43cca59df3d199bc2a869eda714c9e3c097eea941e59138f4aa1fc40b4
MD5 a3c3d2957f4741ebf8d323c1c8f8acd6
BLAKE2b-256 be8a7aa5c6db7292d88bbd15586bea56d4968fa3ecfb2038bdfa2b152f9ca9aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for polarsgrid-0.2.0-py3-none-any.whl:

Publisher: publish.yml on vankesteren/polarsgrid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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