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, 3)
┌────────┬─────┬─────┐
│ row_id ┆ a   ┆ b   │
│ ---    ┆ --- ┆ --- │
│ i64    ┆ i64 ┆ str │
╞════════╪═════╪═════╡
│ 0      ┆ 1   ┆ x   │
│ 1      ┆ 2   ┆ x   │
│ 2      ┆ 1   ┆ y   │
│ 3      ┆ 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:

expand_grid(
    fruit=["apple", "banana", "pear"], 
    color=["red", "green", "blue"], 
    check=[True, False], 
    _categorical=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.1.0.tar.gz (5.6 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.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for polarsgrid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 43b0b83d3074dbd34d184f215356a29e120665d608556babc691e62d2b3ee4a3
MD5 83d6dd1a953f35f17261eeadf547ed04
BLAKE2b-256 1f7e9633b305af7c26043f1ec3cc8a879c7850130e03c802b8031ff4ee3e4021

See more details on using hashes here.

Provenance

The following attestation bundles were made for polarsgrid-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: polarsgrid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5c061d2f22e3dac1337af7fc331aa7b5ae703233f9b7c0a38be6a0b0b886509
MD5 6397c1bf2cc9f4517382c6302aab0d52
BLAKE2b-256 478ae39a84c1281c7b29e50bd9a0965e4a72e57bff79406a5b5dd7e01c114ada

See more details on using hashes here.

Provenance

The following attestation bundles were made for polarsgrid-0.1.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