Skip to main content

tidypyrs

tidypyrs is a data frame library built on top of the blazingly fast polars library that gives access to methods and functions familiar to R tidyverse users.

This library is inspired by tidypolars, as well as continues the work tidypolars left behind.

Installation

You can install tidypyrs with pip:

$ pip install tidypyrs

Or through uv:

$ uv pip install tidypyrs

Documentation

Detailed documentations will be added later in the near future.

General syntax

tidypyrs methods are designed to work like tidyverse functions:

import tidypyrs as tp
from tidypyrs import col as c

tf = tp.TibbleFrame(
    x=range(3), 
    y=range(3, 6), 
    z=["a", "a", "b"]
)

(
    tf
    .select("x", "y", "z")
    .filter(col("x") < 4, c("y") > 1)
    .arrange(desc("z"), "x")
    .mutate(
        double_x=c("x") * 2, 
        x_plus_y=c("x") + c("y")
    )
)
┌─────┬─────┬─────┬──────────┬──────────┐
│ x   ┆ y   ┆ z   ┆ double_x ┆ x_plus_y │
│ --- ┆ --- ┆ --- ┆ ---      ┆ ---      │
│ i64 ┆ i64 ┆ str ┆ i64      ┆ i64      │
╞═════╪═════╪═════╪══════════╪══════════╡
│ 2   ┆ 5   ┆ b   ┆ 4        ┆ 7        │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 0   ┆ 3   ┆ a   ┆ 0        ┆ 3        │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 1   ┆ 4   ┆ a   ┆ 2        ┆ 5        │
└─────┴─────┴─────┴──────────┴──────────┘

The key difference from R is that column names must be wrapped in col() in the following methods:

  • .filter()
  • .mutate()
  • .summarize()

The general idea - when doing calculations on a column you need to wrap it in col(). When doing simple column selections (like in .select()) you can pass the column names as strings.

Fast group-by syntax with over parameter

polars provides polars.Expr.over() to perform quick and short group-by operation without calling group_by().agg(). Library tidypyrs supports the same idea with over parameter.

  • A single column can be passed with over = 'z'
  • Multiple columns can be passed with over = ['y', 'z']
tf = tp.TibbleFrame({
    "x": range(3), 
    "y": ["a", "a", "b"]
})

print(
    tf
    .filter(c("x") <= c("x").mean(), over="y")
    .arrange("y")
)
┌─────┬─────┐
│ x   ┆ y   │
│ --- ┆ --- │
│ f64 ┆ str │
╞═════╪═════╡
│ 0   ┆ a   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ b   │
└─────┴─────┘

Selecting/dropping columns

tidyselect functions can be mixed with normal selection when selecting columns:

tf = tp.TibbleFrame(
    x1=range(3), 
    x2=range(3), 
    y=range(3), 
    z=range(3)
)

tf.select(tp.starts_with("x"), "z")
┌─────┬─────┬─────┐
│ x1  ┆ x2  ┆ z   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 0   ┆ 0   ┆ 0   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 1   ┆ 1   ┆ 1   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 2   ┆ 2   │
└─────┴─────┴─────┘

To drop columns use the .drop() method:

tf.drop(tp.starts_with("x"), "z")
┌─────┐
│ y   │
│ --- │
│ i64 │
╞═════╡
│ 0   │
├╌╌╌╌╌┤
│ 1   │
├╌╌╌╌╌┤
│ 2   │
└─────┘

Providing TibbleLazy as a translated version of polars.LazyFrame

tl = tp.TibbleLazy(
    group=["a", "a", "b"],
    value=[1, 2, 3],
)

result = tl.mutate(
    group_mean=tp.mean("value"),
    over="group",
)

print(result.collect())
┌───────┬───────┬────────────┐
│ group ┆ value ┆ group_mean │
│ ---   ┆ ---   ┆ ---        │
│ str   ┆ i64   ┆ f64        │
╞═══════╪═══════╪════════════╡
│ a     ┆ 1     ┆ 1.5        │
│ a     ┆ 2     ┆ 1.5        │
│ b     ┆ 3     ┆ 3.0        │
└───────┴───────┴────────────┘

Can call TibbleFrame.lazy() to convert it to TibbleLazy,
and can call TibbleLazy.collect() to realize back to TibbleFrame.

f namespace for fast selecting and accessing columns

tidypyrs provides a very convenient f namespace to allow fast selecting and accessing columns. This pushes polars.col() one step further from a mere column expression. This feature is inspired by datar library.

import tidypyrs as tp
import polars as pl
from tidypyrs import f
import numpy as np

# ================================
# Example with tp.as_ordered()
# ================================

tf = (
    tp.TibbleFrame(y=["b", "a", "b"])
    .mutate(y=tp.as_ordered(f.select("y")))
    # Don't need to call `.pipe(lambda f: f.mutate(y = tp.as_ordered(f.select("y"))))`
)

print(isinstance(tf.pull("y").dtype, pl.Enum))  # True

# ================================
# Example with numpy functions
# ================================

tf = (
    tp.TibbleFrame(x=[1, 4, 9])
    .mutate(
        x_root=np.sqrt(f["x"]), 
        x_sin=np.sin(f.x)
    )
)

Converting to/from pandas data frames

If you need to use a package that requires pandas data frames, you can convert from a tidypyrs TibbleFrame/TibbleLazy to a pandas DataFrame.

To do this you'll first need to install pyarrow:

pip install pyarrow

To convert to a pandas DataFrame:

df = tf.as_pandas()

To convert from a pandas DataFrame to a tidypyrs TibbleFrame/TibbleLazy:

tf = tp.as_tf(df)  # TibbleFrame
tl = tp.as_tl(df)  # TibbleLazy

Metadata

Release files for tidypyrs 0.4.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 tidypyrs 0.4.0
File Size Uploaded Provenance
tidypyrs-0.4.0.tar.gz 30.9 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 66.0 kB

Release files / tidypyrs-0.4.0.tar.gz

Download URL tidypyrs-0.4.0.tar.gz
Size 30.9 kB
Tags Source
SHA-256 checksum
How to use checksums
91d9a085946fdb967dca7fefff2235d11ae55cfa52aa22544b77497237411f7c
BLAKE2b-256 checksum
How to use checksums
dccfbe00a87ab4cb69e1f53ebc265b1cc612033265027c76681e296f7077e7b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / tidypyrs-0.4.0-py3-none-any.whl

Download URL tidypyrs-0.4.0-py3-none-any.whl
Size 35.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77e2a885fdfb0cb0122bcf900e0e9266046df17ef3bb0432415c9ca2ab3ab4ae
BLAKE2b-256 checksum
How to use checksums
2a95040ca417806443e4316832020520f60f34b0c92890fbf28758ed2a739679
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for tidypyrs 0.4.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.4.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