Skip to main content

A Polars plugin for easily reordering DataFrame columns

Project description

Polars Permute Plugin

A Polars plugin for easily reordering DataFrame columns.

Supports column permutations like prepending, appending, shifting, and swapping.

Installation

pip install polars-permute[polars]

On older CPUs run:

pip install polars-permute[polars-lts-cpu]

Features

  • Supports both string column names and Polars expressions
  • Handles single or multiple columns
  • Maintains relative ordering of moved columns
  • Chain operations together
  • Gracefully handles edge cases (non-existent columns, empty inputs)

Usage

The plugin adds a permute namespace to Polars DataFrames with methods for column reordering:

import polars as pl
import polars_permute

# Create a sample DataFrame
df = pl.DataFrame({
    "a": [1, 2, 3],
    "b": [4, 5, 6],
    "c": [7, 8, 9],
    "d": [10, 11, 12]
})

# Move column 'd' to the start
df.permute.prepend("d")

# Move multiple columns to the end
df.permute.append(["a", "b"])

# Move columns to a specific position
df.permute.at(["b", "c"], index=0)

# Shift columns left/right
df.permute.shift("a", "b", steps=1, direction="right")

# Swap two columns
df.permute.swap("a", "d")

# Move columns before or after another column
df.permute.before("d", "b")  # Move 'd' before 'b'
df.permute.after(["a", "b"], "c")  # Move 'a' and 'b' after 'c'

API Reference

prepend(cols)

Move specified column(s) to the start (index 0).

df.permute.prepend("d")  # Single column
df.permute.prepend(["c", "d"])  # Multiple columns
df.permute.prepend(pl.col("a").alias("x"))  # Using expressions

append(cols)

Move specified column(s) to the end.

df.permute.append("a")
df.permute.append(["a", "b"])

at(cols, index)

Move specified column(s) to exact position.

df.permute.at("d", 1)  # Move 'd' to index 1
df.permute.at(["b", "c"], 0)  # Move multiple columns

shift(cols, steps=1, direction="left")

Shift column(s) left or right by steps.

df.permute.shift("c", steps=1, direction="left")
df.permute.shift("a", "b", steps=2, direction="right")

swap(col1, col2)

Swap positions of two columns.

df.permute.swap("a", "d")

before(cols, reference)

Move specified column(s) before a reference column.

df.permute.before("d", "b")  # Move 'd' before 'b'
df.permute.before(["c", "d"], "a")  # Move multiple columns before 'a'

after(cols, reference)

Move specified column(s) after a reference column.

df.permute.after("a", "c")  # Move 'a' after 'c'
df.permute.after(["a", "b"], "d")  # Move multiple columns after 'd'

Notes

  • Operations create a new DataFrame; the original is not modified
  • Column order is preserved for multiple column operations
  • Invalid columns are ignored gracefully
  • Out-of-bounds indexes are clamped to valid range

Contributing

Feel free to open issues or submit pull requests for improvements or bug fixes.

License

MIT License

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

polars_permute-0.1.5.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

polars_permute-0.1.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file polars_permute-0.1.5.tar.gz.

File metadata

  • Download URL: polars_permute-0.1.5.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_permute-0.1.5.tar.gz
Algorithm Hash digest
SHA256 6204f363632bba88b963f94212205e0ef6cf08188bdc52fbbdd61a52948e87a9
MD5 ef41ca3eb22203a81f7b500a6844c260
BLAKE2b-256 2062ce5a02d22081eea3ef0f6008b10f803b9475cebeaf5844aa362dc0caf323

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_permute-0.1.5.tar.gz:

Publisher: CI.yml on lmmx/polars-permute-plugin

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

File details

Details for the file polars_permute-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: polars_permute-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_permute-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4561d27b17f09f6d28d49323b19e02e9ac19f670b68012c3d70bf4f5f0736015
MD5 1658402f1b20fe6d3ad57d335f3ee20d
BLAKE2b-256 a1e7b253dd38ec21df8e93bc785f7db55b1fd67132e023821f40988407585b00

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_permute-0.1.5-py3-none-any.whl:

Publisher: CI.yml on lmmx/polars-permute-plugin

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