Narwhals

2.18.1 · active · verified Sat Mar 28

Narwhals is an extremely lightweight and extensible compatibility layer between dataframe libraries. It provides a unified API, largely mirroring the Polars API, enabling users to write dataframe-agnostic code that works across various backends such as pandas, Polars, cuDF, PyArrow, Dask, DuckDB, Ibis, PySpark, and SQLFrame. It is currently at version 2.18.1 and maintains an active development cycle with frequent releases, often including weekly or bi-weekly updates for bug fixes and minor enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write a dataframe-agnostic function using Narwhals. The function `process_data` accepts any supported native dataframe type (e.g., pandas DataFrame, Polars DataFrame) via `nw.from_native`, applies a group-by and aggregation operation using Narwhals' Polars-like API, and then converts the result back to the original native dataframe type using `to_native()`.

import narwhals as nw
import pandas as pd
import polars as pl
from narwhals.typing import IntoFrameT

def process_data(df_native: IntoFrameT) -> IntoFrameT:
    df = nw.from_native(df_native)
    result = (
        df.group_by(nw.col('category'))
        .agg(nw.col('value').mean().alias('mean_value'))
        .sort('mean_value', descending=True)
    )
    return result.to_native()

# Example with pandas
pd_df = pd.DataFrame({'category': ['A', 'B', 'A', 'C'], 'value': [10, 20, 15, 25]})
pd_result = process_data(pd_df)
print('Pandas Result:')
print(pd_result)

# Example with polars
pl_df = pl.DataFrame({'category': ['A', 'B', 'A', 'C'], 'value': [10, 20, 15, 25]})
pl_result = process_data(pl_df)
print('\nPolars Result:')
print(pl_result)

view raw JSON →