Skip to main content

kra

A set of useful tools to work with polars, providing convenient extensions for DataFrame manipulation, column operations, label encoding, and more.

Installation

Build and install the Rust extension and Python API using maturin:

pip install maturin
maturin develop

Or, for development and testing:

pip install nox
nox

Features

  • DataFrame and Series extensions: Add new methods to polars DataFrames and Series.
  • Column utilities: Easily rename, check, and transform DataFrame columns.
  • Label encoding: Encode string labels as categorical/integer values.
  • Dict-of-dicts conversion: Convert between DataFrames and nested dictionaries.

Example Use Cases

1. Dict-of-Dicts Conversion

Convert a DataFrame to a dict of dicts using a column as the key:

import polars as pl
import kra

df = pl.DataFrame({
    "id": [1, 2, 3],
    "name": ["Alice", "Bob", "Charlie"]
})

dod = df.to_dod("id")
# {1: {'id': 1, 'name': 'Alice'}, 2: {'id': 2, 'name': 'Bob'}, ...}

# Convert back:
df2 = kra.from_dod(dod, "id")

2. Column Name Transformations

Transform column names to different cases:

import polars as pl
import kra

df = pl.DataFrame({
    "First Name": [1, 2],
    "Last Name": [3, 4]
})

df_lower = df.cols.to_lowercase()
df_camel = df.cols.to_camelcalse()
df_snake = df.cols.to_snakecase()

3. Label Encoding

Encode string labels as integers:

import polars as pl
import kra

df = pl.DataFrame({
    "label": ["cat", "dog", "cat", "bird"]
})

# Series API
encoded = df["label"].label.encode()

# Expression API (for use in with_columns, etc.)
df2 = df.with_columns(
    pl.col("label").label.encode().alias("encoded_label")
)

4. DataFrame Utilities

Drop columns of type Null:

import polars as pl
import kra

df = pl.DataFrame({
    "a": [1, 2, 3],
    "b": [None, None, None]
})

df_clean = df.drop_null_cols()

5. From Array-like

Create a DataFrame from a numpy array:

import kra
import numpy as np

data = np.array([[1, 2], [3, 4]])
df = kra.from_arraylike(data, schema=["x", "y"], orient="col")

API Reference

  • kra.from_dod: Create DataFrame from dict of dicts.
  • kra.to_dod: Convert DataFrame to dict of dicts.
  • kra.Cols: DataFrame column utilities (access via df.cols).
  • kra.LabelSeries: Series label encoding (access via series.label).
  • kra.LabelExpr: Expression label encoding (access via pl.col(...).label).
  • kra.drop_null_cols: Remove columns of type Null.
  • kra.from_arraylike: Create DataFrame from array-like objects.

For more, see the intro.ipynb notebook.


Rust Extension

kra includes a Rust extension for fast label encoding, accessible via the Python API.


License

MIT License. See LICENSE for details.

Metadata

Release files for kra 0.1.0

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

Source distribution (sdist)

Source distribution for kra 0.1.0
File Size Uploaded Provenance Actions
kra-0.1.0.tar.gz 10.9 kB Without provenance Details for kra-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kra 0.1.0
File Interpreter ABI Platform Actions
kra-0.1.0-py3-none-any.whl Python 3 none any Details for kra-0.1.0-py3-none-any.whl

Total release size: 21.0 kB

Release files / kra-0.1.0.tar.gz

Download URL kra-0.1.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
263503d7ed1a834750999faf808c52f573b4beb14b3038aa78509aec96044bc1
BLAKE2b-256 checksum
How to use checksums
442df6ec316dd7781f4c1ff82b5c5c2e1a39f28281c24658513d48a25b3b6b8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release files / kra-0.1.0-py3-none-any.whl

Download URL kra-0.1.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c54c60a9704a20aa22c024e61befd5a19eaa716183ddc4fbd534932f8d535941
BLAKE2b-256 checksum
How to use checksums
93fa66e288a07f74493d0f78e4bd3309f37a677e8130b6daa4eb1c72535c7058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Security for kra 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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