Skip to main content

Extremely lightweight compatibility layer between pandas, Polars, cuDF, and Modin

Project description

Narwhals

narwhals_small

PyPI version Documentation

Extremely lightweight and extensible compatibility layer between Polars, pandas, modin, and cuDF (and more!).

Seamlessly support all, without depending on any!

  • Just use a subset of the Polars API, no need to learn anything new
  • No dependencies (not even Polars), keep your library lightweight
  • ✅ Separate lazy and eager APIs
  • ✅ Use Polars Expressions
  • ✅ 100% branch coverage, tested against pandas and Polars nightly builds!

Installation

pip install narwhals

Or just vendor it, it's only a bunch of pure-Python files.

Usage

There are three steps to writing dataframe-agnostic code using Narwhals:

  1. use narwhals.from_native to wrap a pandas/Polars/Modin/cuDF DataFrame/LazyFrame in a Narwhals class

  2. use the subset of the Polars API supported by Narwhals

  3. use narwhals.to_native to return an object to the user in its original dataframe flavour. For example:

    • if you started with pandas, you'll get pandas back
    • if you started with Polars, you'll get Polars back
    • if you started with Modin, you'll get Modin back (and compute will be distributed)
    • if you started with cuDF, you'll get cuDF back (and compute will happen on GPU)

Example

Here's an example of a dataframe agnostic function:

from typing import Any
import pandas as pd
import polars as pl

import narwhals as nw


def my_agnostic_function(
    suppliers_native,
    parts_native,
):
    suppliers = nw.from_native(suppliers_native)
    parts = nw.from_native(parts_native)

    result = (
        suppliers.join(parts, left_on="city", right_on="city")
        .filter(nw.col("weight") > 10)
        .group_by("s")
        .agg(
            weight_mean=nw.col("weight").mean(),
            weight_max=nw.col("weight").max(),
        )
    )

    return nw.to_native(result)

You can pass in a pandas or Polars dataframe, the output will be the same! Let's try it out:

suppliers = {
    "s": ["S1", "S2", "S3", "S4", "S5"],
    "sname": ["Smith", "Jones", "Blake", "Clark", "Adams"],
    "status": [20, 10, 30, 20, 30],
    "city": ["London", "Paris", "Paris", "London", "Athens"],
}
parts = {
    "p": ["P1", "P2", "P3", "P4", "P5", "P6"],
    "pname": ["Nut", "Bolt", "Screw", "Screw", "Cam", "Cog"],
    "color": ["Red", "Green", "Blue", "Red", "Blue", "Red"],
    "weight": [12.0, 17.0, 17.0, 14.0, 12.0, 19.0],
    "city": ["London", "Paris", "Oslo", "London", "Paris", "London"],
}

print("pandas output:")
print(
    my_agnostic_function(
        pd.DataFrame(suppliers),
        pd.DataFrame(parts),
    )
)
print("\nPolars output:")
print(
    my_agnostic_function(
        pl.LazyFrame(suppliers),
        pl.LazyFrame(parts),
    ).collect()
)
pandas output:
    s  weight_mean  weight_max
0  S1         15.0        19.0
1  S2         14.5        17.0
2  S3         14.5        17.0
3  S4         15.0        19.0

Polars output:
shape: (4, 3)
┌─────┬─────────────┬────────────┐
│ s   ┆ weight_mean ┆ weight_max │
│ --- ┆ ---         ┆ ---        │
│ str ┆ f64         ┆ f64        │
╞═════╪═════════════╪════════════╡
│ S2  ┆ 14.5        ┆ 17.0       │
│ S3  ┆ 14.5        ┆ 17.0       │
│ S4  ┆ 15.0        ┆ 19.0       │
│ S1  ┆ 15.0        ┆ 19.0       │
└─────┴─────────────┴────────────┘

Magic! 🪄

Scope

  • Do you maintain a dataframe-consuming library?
  • Is there a Polars function which you'd like Narwhals to have, which would make your work easier?

If, I'd love to hear from you!

Note: You might suspect that this is a secret ploy to infiltrate the Polars API everywhere. Indeed, you may suspect that.

Why "Narwhals"?

Because they are so awesome.

Thanks to Olha Urdeichuk for the illustration!

Project details


Release history Release notifications | RSS feed

This version

0.7.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

narwhals-0.7.9.tar.gz (291.6 kB view details)

Uploaded Source

Built Distribution

narwhals-0.7.9-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file narwhals-0.7.9.tar.gz.

File metadata

  • Download URL: narwhals-0.7.9.tar.gz
  • Upload date:
  • Size: 291.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for narwhals-0.7.9.tar.gz
Algorithm Hash digest
SHA256 b2eb7213c5e65b501466141d89b7ea09f3b9d58f289dfa078087b82672517984
MD5 187610ac9eeaf11164c52f22f3cc461f
BLAKE2b-256 8eb0b325b1fa81ccb08d9d018240ecf5c29d300745e2541cbc4f1aef9cc4a06a

See more details on using hashes here.

Provenance

File details

Details for the file narwhals-0.7.9-py3-none-any.whl.

File metadata

  • Download URL: narwhals-0.7.9-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for narwhals-0.7.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d5474b0d82a36bb7f68537004836c9c6c8ddd0bd7d5f856e07b61aa61e4f692c
MD5 a154e44f76830dd98a256364a4fc69dc
BLAKE2b-256 75f981f8ddaa506f0688b8d9c9039c0b82b815cc812d353d8c8b4291f28c67f4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page