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:

from polarsgrid import expand_grid_lazy
lgrid = expand_grid_lazy(
    sample_size=list(range(1000)), 
    condition=["a", "b", "c"], 
    iteration=list(range(500)), 
)
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.3.0.tar.gz (3.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.3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: polarsgrid-0.3.0.tar.gz
  • Upload date:
  • Size: 3.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.3.0.tar.gz
Algorithm Hash digest
SHA256 b27da192d4e9249911482fcb7938556bb0bb32f00fa6f169869183e4650de4dd
MD5 0790b9a676685467ef149261a6d7df48
BLAKE2b-256 48b93160bdc38f27c0ff51ed31f899c52f2d5170d163ebdb4180db75a8eabdbf

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: polarsgrid-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb9b04872c8d88fbdb666e9b0c08ef2ea5aafe37887543bbaa2ee9d84b47394e
MD5 3e86c217ca3cfc125b15e5321ad64975
BLAKE2b-256 d9889eab8db7a135fa601e0fe9355eeb256aef7ba8094f7880c57c51861b8276

See more details on using hashes here.

Provenance

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