Skip to main content

Extremely lightweight compatibility layer between dataframe libraries

Project description

Narwhals

narwhals_small

PyPI version Downloads Trusted publishing PYPI - Types LFX Health Score OpenSSF Scorecard

Extremely lightweight and extensible compatibility layer between dataframe libraries!

  • Full API support: cuDF, Modin, pandas, Polars, PyArrow.
  • Lazy-only support: Daft, Dask, DuckDB, Ibis, PySpark, SQLFrame.

Seamlessly support all, without depending on any!

  • Just use a subset of the Polars API, no need to learn anything new
  • Zero dependencies, Narwhals only uses what the user passes in so your library can stay lightweight
  • ✅ Separate lazy and eager APIs, use expressions
  • ✅ Support pandas' complicated type system and index, without either getting in the way
  • 100% branch coverage, tested against pandas and Polars nightly builds
  • Negligible overhead, see overhead
  • ✅ Let your IDE help you thanks to full static typing, see typing
  • Perfect backwards compatibility policy, see stable api for how to opt-in

Get started!

Table of contents

Installation

  • pip (recommended, as it's the most up-to-date)
    pip install narwhals
    
  • conda-forge (also fine, but the latest version may take longer to appear)
    conda install -c conda-forge narwhals
    

Usage

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

  1. use narwhals.from_native to wrap a pandas/Polars/Modin/cuDF/PyArrow 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)
    • if you started with PyArrow, you'll get PyArrow back

narwhals_gif

Example

Narwhals allows you to define dataframe-agnostic functions. For example:

import narwhals as nw
from narwhals.typing import IntoFrameT


def agnostic_function(df_native: IntoFrameT) -> IntoFrameT:
    return (
        nw.from_native(df_native)
        .with_columns(
            category=nw.when(nw.col("animal").str.contains("whale"))
            .then(nw.lit("whale"))
            .otherwise(nw.lit("other"))
        )
        .to_native()
    )

You can then pass pandas.DataFrame, polars.DataFrame, polars.LazyFrame, duckdb.DuckDBPyRelation, pyspark.sql.DataFrame, pyarrow.Table, and more, to agnostic_function. In each case, no additional dependencies will be required, and computation will stay native to the input library:

import duckdb
import polars as pl
import pandas as pd

data = {
    "animal": ["blue whale", "orca", "dolphin", "humpback whale", "seal"],
    "length_m": [30.0, 8, 2.5, 17, 2.2],
    "weight_kg": [150000, 4000, 200, 30000, 85],
}

print("Polars result")
df_pl = pl.DataFrame(data)
print(agnostic_function(df_pl))

print("DuckDB result")
print(agnostic_function(duckdb.sql("select * from df_pl")))

print("pandas result")
df_pd = pd.DataFrame(data)
print(agnostic_function(df_pd))
Polars result
shape: (5, 4)
┌────────────────┬──────────┬───────────┬──────────┐
│ animal         ┆ length_m ┆ weight_kg ┆ category │
│ ---            ┆ ---      ┆ ---       ┆ ---      │
│ str            ┆ f64      ┆ i64       ┆ str      │
╞════════════════╪══════════╪═══════════╪══════════╡
│ blue whale     ┆ 30.0     ┆ 150000    ┆ whale    │
│ orca           ┆ 8.0      ┆ 4000      ┆ other    │
│ dolphin        ┆ 2.5      ┆ 200       ┆ other    │
│ humpback whale ┆ 17.0     ┆ 30000     ┆ whale    │
│ seal           ┆ 2.2      ┆ 85        ┆ other    │
└────────────────┴──────────┴───────────┴──────────┘
DuckDB result
┌────────────────┬──────────┬───────────┬──────────┐
│     animal     │ length_m │ weight_kg │ category │
│    varchar     │  double  │   int64   │ varchar  │
├────────────────┼──────────┼───────────┼──────────┤
│ blue whale     │     30.0 │    150000 │ whale    │
│ orca           │      8.0 │      4000 │ other    │
│ dolphin        │      2.5 │       200 │ other    │
│ humpback whale │     17.0 │     30000 │ whale    │
│ seal           │      2.2 │        85 │ other    │
└────────────────┴──────────┴───────────┴──────────┘

pandas result
           animal  length_m  weight_kg category
0      blue whale      30.0     150000    whale
1            orca       8.0       4000    other
2         dolphin       2.5        200    other
3  humpback whale      17.0      30000    whale
4            seal       2.2         85    other

See the tutorial for several examples!

Scope

  • Do you maintain a dataframe-consuming library?
  • Do you have a specific Polars function in mind that you would like Narwhals to have in order to make your work easier?

If you said yes to both, we'd love to hear from you!

Roadmap

See roadmap discussion on GitHub for an up-to-date plan of future work.

Used by

Join the party!

Feel free to add your project to the list if it's missing, and/or chat with us on Discord if you'd like any support.

Sponsors and institutional partners

Narwhals is 100% independent, community-driven, and community-owned. We are extremely grateful to the following organisations for having provided some funding / development time:

If you contribute to Narwhals on your organization's time, please let us know. We'd be happy to add your employer to this list!

Support

If you'd like to say "thank you", please give us a ⭐ star ⭐.

Please contact hello_narwhals@proton.me if you would like to:

  • Receive professional support (e.g., if you're using or would like to use Narwhals at your company).
  • Have any Narwhals fixes / features prioritised.
  • Commission any Narwhals plugins for new backends.

Appears on

Narwhals has been featured in several talks, podcasts, and blog posts:

Why "Narwhals"?

Coz they are so awesome.

Thanks to Olha Urdeichuk for the illustration!

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

narwhals-2.21.2.tar.gz (632.2 kB view details)

Uploaded Source

Built Distribution

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

narwhals-2.21.2-py3-none-any.whl (451.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: narwhals-2.21.2.tar.gz
  • Upload date:
  • Size: 632.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for narwhals-2.21.2.tar.gz
Algorithm Hash digest
SHA256 5c5b2d0b47aef7c73ea412cfcbcd467f2f2d5be73e3c2ab19d78f4a97718790a
MD5 727208022bd0c9067f56d2fcd8eb2a51
BLAKE2b-256 cfa06198c56d42ef2f3c6ed0c42ba30dbcefdc86a91262d7d449010770ae085b

See more details on using hashes here.

Provenance

The following attestation bundles were made for narwhals-2.21.2.tar.gz:

Publisher: publish_to_pypi.yml on narwhals-dev/narwhals

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

File details

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

File metadata

  • Download URL: narwhals-2.21.2-py3-none-any.whl
  • Upload date:
  • Size: 451.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for narwhals-2.21.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7bb57c3700486039215455b9bf2d64261915cc0fd845cc30272d631df696b251
MD5 882e1f66b702ea9b3f6d86972d0d857c
BLAKE2b-256 1d77928ea2e70641ca177a11140062cc5840d421795f2e82749d408d0cce900a

See more details on using hashes here.

Provenance

The following attestation bundles were made for narwhals-2.21.2-py3-none-any.whl:

Publisher: publish_to_pypi.yml on narwhals-dev/narwhals

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