Skip to main content

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'

# Sort columns alphabetically
df.permute.sort()
df.permute.sort(descending=True)

# Natural sort (requires: pip install polars-permute[natsort])
df.permute.natsort()  # col1, col2, col10 (not col1, col10, col2)

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'

sort(descending=False)

Sort columns lexicographically.

df.permute.sort()
df.permute.sort(descending=True)

natsort(descending=False)

Sort columns using natural sort order. Requires natsort extra.

# pip install polars-permute[natsort]
df.permute.natsort()  # ["col1", "col2", "col10"] not ["col1", "col10", "col2"]

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

Metadata

Release files for polars-permute 0.1.6

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

Source distribution (sdist)

Source distribution for polars-permute 0.1.6
File Size Uploaded
polars_permute-0.1.6.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for polars-permute 0.1.6
File Interpreter ABI Platform
polars_permute-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / polars_permute-0.1.6.tar.gz

Download URL polars_permute-0.1.6.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
395230a28e3f8193198ff6b1b4dfa673ac3ff47942b72b112e4d66a2824b0c05
BLAKE2b-256 checksum
How to use checksums
9c99fcd514c93ab6e0683e168fa8ecbe9e7d248892bea83bdbae2c183ae305e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 5, 2026.

Transparency log

Release files / polars_permute-0.1.6-py3-none-any.whl

Download URL polars_permute-0.1.6-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44a56fc7ffb0d1035c95b0496830f2389f70f8b903b7c67314f497b4e7a51efe
BLAKE2b-256 checksum
How to use checksums
15d13b8e92398c78851258ef7b8d11fbe9df15f858b880ae59d28ac9d206326e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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