### Example Usage with PyArrow Table Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/series.md Demonstrates calling the `my_func` with a PyArrow Table. Ensure PyArrow is installed. ```python import pyarrow as pa table = pa.table({"a": [-1, 1, 3], "b": [3, 5, -3]}) print(my_func(table)) ``` -------------------------------- ### Example Usage with Pandas DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/series.md Demonstrates calling the `my_func` with a pandas DataFrame. Ensure pandas is installed. ```python import pandas as pd df = pd.DataFrame({"a": [-1, 1, 3], "b": [3, 5, -3]}) print(my_func(df)) ``` -------------------------------- ### Example Git Clone Command Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md An example of how to clone the Narwhals repository, replacing YOUR-GITHUB-USERNAME with your actual username. ```bash git clone git@github.com:YOUR-GITHUB-USERNAME/narwhals.git narwhals-dev ``` -------------------------------- ### Install Narwhals with extras using pip Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Install Narwhals with specified backend extras using pip. ```bash python -m pip install "narwhals[polars,pyarrow]" ``` -------------------------------- ### Install Narwhals with extras using uv Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Install Narwhals along with specific backend extras (e.g., polars, pyarrow) using uv. ```bash uv add "narwhals[polars,pyarrow]" ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Install the prek tool as a git hook to automatically format and lint code before each commit. ```bash uv run prek install ``` -------------------------------- ### Install Narwhals using pip Source: https://github.com/narwhals-dev/narwhals/blob/main/README.md Install Narwhals using pip for the most up-to-date version. This is the recommended installation method. ```bash pip install narwhals ``` -------------------------------- ### Example Docstring with PyArrow Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md This example demonstrates how to write a docstring for a Narwhals function. It imports PyArrow, creates a PyArrow table, converts it to a Narwhals DataFrame, and calls an example method. ```python >>> import pyarrow as pa >>> import narwhals as nw >>> df_native = pa.table({"foo": [1, 2], "bar": [6.0, 7.0]}) >>> df = nw.from_native(df_native) >>> df.estimated_size() 32 ``` -------------------------------- ### Example Usage with Polars Eager DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/series.md Demonstrates calling the `my_func` with a Polars eager DataFrame. Ensure Polars is installed. ```python import polars as pl df = pl.DataFrame({"a": [-1, 1, 3], "b": [3, 5, -3]}) print(my_func(df)) ``` -------------------------------- ### Inline Script Dependencies with uv Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md This example shows how to declare inline script dependencies for Narwhals using uv. This allows you to run Python scripts without needing to manually install the environment. ```python # /// script # dependencies = ["narwhals[pandas,polars,pyarrow]"] # /// ``` -------------------------------- ### Install Narwhals with extras using Poetry Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Add Narwhals with specific backend extras to a Poetry project. ```bash poetry add "narwhals[polars,pyarrow]" ``` -------------------------------- ### Install Narwhals with uv pip Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Install Narwhals into an existing virtual environment without modifying pyproject.toml. ```bash uv pip install narwhals ``` -------------------------------- ### Create DataFrames in Various Formats Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe_conversion.md Demonstrates creating DataFrames in Polars, pandas, and DuckDB formats for conversion testing. Ensure necessary libraries are installed. ```python import narwhals as nw from narwhals.typing import IntoDataFrame from typing import Any import duckdb import polars as pl import pandas as pd df_polars = pl.DataFrame( { "A": [1, 2, 3, 4, 5], "fruits": ["banana", "banana", "apple", "apple", "banana"], "B": [5, 4, 3, 2, 1], "cars": ["beetle", "audi", "beetle", "beetle", "beetle"], } ) df_pandas = df_polars.to_pandas() df_duckdb = duckdb.sql("SELECT * FROM df_polars") ``` -------------------------------- ### Verify Narwhals Installation Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Execute this Python code in the REPL to confirm that Narwhals has been installed successfully by printing its version number. ```python import narwhals print(narwhals.__version__) ``` -------------------------------- ### Testing with PyArrow Table Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet demonstrates using the `func` with a PyArrow Table. Ensure PyArrow is installed and imported. ```python import pyarrow as pa table = pa.table({"a": [1, 1, 2]}) print(func(table)) ``` -------------------------------- ### Check Git Version Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Verify if Git is installed on your machine by checking its version. ```bash git --version ``` -------------------------------- ### Example Usage with Polars Lazy DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/series.md Demonstrates calling the `my_func` with a Polars lazy DataFrame. Ensure Polars is installed. The `.collect()` method is used to trigger computation. ```python import polars as pl df = pl.LazyFrame({"a": [-1, 1, 3], "b": [3, 5, -3]}) print(my_func(df).collect()) ``` -------------------------------- ### Install Dask and Modin Dependencies Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md To run tests for Dask and Modin locally, sync the environment with their respective extras using `uv sync`. This command updates `pyproject.toml` and `uv.lock`. ```terminal uv sync --group local-dev --extra dask --extra modin ``` -------------------------------- ### Generate SQL via Ibis Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/generating_sql.md This example shows how to generate SQL for the `avg_monthly_price` function using the Ibis library. It converts a Narwhals DataFrame to an Ibis table and then uses `ibis.to_sql`. ```python import ibis df = nw.from_native(ibis.table({"date": "date", "price": "double"}, name="prices")) print(ibis.to_sql(avg_monthly_price(df).to_native())) ``` -------------------------------- ### Install Narwhals with pip Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Install Narwhals using pip after creating and activating a Python 3.10+ virtual environment. Note that this does not modify pyproject.toml. ```bash python -m pip install narwhals ``` -------------------------------- ### Generate Pretty SQL for Monthly Average Price Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/generating_sql.md This snippet shows how to generate formatted SQL using `result.to_sql(pretty=True)`. This requires the `sqlparse` library to be installed for pretty-printing. ```python print(result.to_sql(pretty=True)) ``` -------------------------------- ### Testing Group-by with PyArrow Table Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet demonstrates using the group-by `func` with a PyArrow Table. Ensure PyArrow is installed and imported. ```python import pyarrow as pa table = pa.table({"a": [1, 1, 2], "b": [4, 5, 6]}) print(func(table)) ``` -------------------------------- ### Add Narwhals and development extras using uv Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md For library authors, add Narwhals as a runtime dependency and specific backends as development extras using uv. This prevents consumers from being forced to install backends. ```bash uv add narwhals uv add --group dev "narwhals[polars,pyarrow]" ``` -------------------------------- ### Testing with Pandas DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet shows how to use the `func` with a Pandas DataFrame. Ensure Pandas is installed and imported. ```python import pandas as pd df = pd.DataFrame({"a": [1, 1, 2]}) print(func(df)) ``` -------------------------------- ### Group-by and Mean with from/to_native Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This example demonstrates using `nw.from_native` and `nw.to_native` for group-by operations. It converts the input to a Narwhals DataFrame, performs a group-by aggregation, sorts the result, and converts it back to the native format. ```python import narwhals as nw from narwhals.typing import IntoFrameT def func(df: IntoFrameT) -> IntoFrameT: return (nw.from_native(df).group_by("a").agg(nw.col("b").mean()).sort("a").to_native()) ``` -------------------------------- ### Fetch Latest Changes Source: https://github.com/narwhals-dev/narwhals/wiki/Home Update your local main branch with the latest changes from the upstream repository before starting the release process. ```bash git checkout main git fetch upstream git reset --hard upstream/main ``` -------------------------------- ### Elementwise Push-down Example in Narwhals Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Demonstrates how Narwhals rewrites expressions to push 'over' nodes past elementwise operations for SQL compatibility. This allows operations that would otherwise be invalid in SQL engines. ```python import narwhals as nw print(nw.col("a").sum().abs().over("b")) ``` -------------------------------- ### Testing Group-by with Pandas DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet shows how to use the group-by `func` with a Pandas DataFrame. Ensure Pandas is installed and imported. ```python import pandas as pd df = pd.DataFrame({"a": [1, 1, 2], "b": [4, 5, 6]}) print(func(df)) ``` -------------------------------- ### Install Narwhals using conda-forge Source: https://github.com/narwhals-dev/narwhals/blob/main/README.md Install Narwhals using conda-forge. Note that the latest version may take longer to appear on conda-forge compared to pip. ```bash conda install -c conda-forge narwhals ``` -------------------------------- ### Add Narwhals and development extras using Poetry Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md For library authors, add Narwhals as a runtime dependency and specific backends as development extras using Poetry. This ensures consumers only install Narwhals unless they opt-in. ```bash poetry add narwhals poetry add --group dev "narwhals[polars,pyarrow]" ``` -------------------------------- ### Dependency Checkers and Getters Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/api-reference/dependencies.md This section details functions for checking the availability of specific libraries and retrieving them if available. These functions are useful for conditional logic based on installed dependencies. ```APIDOC ## Dependency Functions ### Description Provides functions to check for the presence of various data manipulation libraries and to retrieve them. ### Functions - **`get_cudf()`**: Retrieves the cuDF library if installed. - **`get_ibis()`**: Retrieves the Ibis library if installed. - **`get_modin()`**: Retrieves the Modin library if installed. - **`get_pandas()`**: Retrieves the Pandas library if installed. - **`get_polars()`**: Retrieves the Polars library if installed. - **`get_pyarrow()`**: Retrieves the PyArrow library if installed. ### Type Checkers - **`is_cudf_dataframe(obj)`**: Checks if an object is a cuDF DataFrame. - **`is_cudf_index(obj)`**: Checks if an object is a cuDF Index. - **`is_cudf_series(obj)`**: Checks if an object is a cuDF Series. - **`is_dask_dataframe(obj)`**: Checks if an object is a Dask DataFrame. - **`is_ibis_table(obj)`**: Checks if an object is an Ibis Table. - **`is_into_dataframe(obj)`**: Checks if an object can be converted into a DataFrame. - **`is_into_series(obj)`**: Checks if an object can be converted into a Series. - **`is_modin_dataframe(obj)`**: Checks if an object is a Modin DataFrame. - **`is_modin_index(obj)`**: Checks if an object is a Modin Index. - **`is_modin_series(obj)`**: Checks if an object is a Modin Series. - **`is_numpy_array(obj)`**: Checks if an object is a NumPy array. - **`is_pandas_dataframe(obj)`**: Checks if an object is a Pandas DataFrame. - **`is_pandas_index(obj)`**: Checks if an object is a Pandas Index. - **`is_pandas_like_dataframe(obj)`**: Checks if an object is a Pandas-like DataFrame. - **`is_pandas_like_index(obj)`**: Checks if an object is a Pandas-like Index. - **`is_pandas_like_series(obj)`**: Checks if an object is a Pandas-like Series. - **`is_pandas_series(obj)`**: Checks if an object is a Pandas Series. - **`is_polars_dataframe(obj)`**: Checks if an object is a Polars DataFrame. - **`is_polars_lazyframe(obj)`**: Checks if an object is a Polars LazyFrame. - **`is_polars_series(obj)`**: Checks if an object is a Polars Series. - **`is_pyarrow_chunked_array(obj)`**: Checks if an object is a PyArrow ChunkedArray. - **`is_pyarrow_table(obj)`**: Checks if an object is a PyArrow Table. ``` -------------------------------- ### Testing with Polars Eager DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet demonstrates using the `func` with an eager Polars DataFrame. Ensure Polars is installed and imported. ```python import polars as pl df = pl.DataFrame({"a": [1, 1, 2]}) print(func(df)) ``` -------------------------------- ### Inspecting a Specific Node (Standard Deviation) Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Examine the details of a specific operation node within an expression. This example shows how to view the configuration of the standard deviation operation, including its function name and keyword arguments. ```python print(expr._nodes[2].as_dict()) ``` -------------------------------- ### Accessing Nested Expression Nodes Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Demonstrates that expressions can be nested, and their constituent operations can be accessed recursively. This example shows how to view the nodes of an expression that is part of another expression's arguments. ```python print(expr._nodes[3].exprs[0]._nodes) ``` -------------------------------- ### Generate SQL for Monthly Average Price using narwhals.sql Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/generating_sql.md This snippet demonstrates generating SQL from a Narwhals DataFrame expression using the `narwhals.sql` module. It requires DuckDB to be installed. The generated SQL follows DuckDB's dialect. ```python import narwhals as nw from narwhals.sql import table prices = table("prices", {"date": nw.Date, "price": nw.Float64}) result = ( prices.group_by(nw.col("date").dt.truncate("1mo")) .agg(nw.col("price").mean()) .sort("date") ) print(result.to_sql()) ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Run `make docs-serve` to build and serve the documentation locally. The docs will automatically refresh on changes. If they don't, use `make docs-clean-serve`. ```terminal make docs-serve ``` ```terminal make docs-clean-serve ``` -------------------------------- ### Activate Virtual Environment (Windows) Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Activate the project's virtual environment manually on Windows. ```bash .\.venv\Scripts\activate ``` -------------------------------- ### Activate Virtual Environment (Linux/macOS) Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Activate the project's virtual environment manually on Linux or macOS. ```bash . .venv/bin/activate ``` -------------------------------- ### Sync Local Development Environment Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Synchronize the project dependencies for local development using uv, including the 'local-dev' group. ```bash uv sync --group local-dev ``` -------------------------------- ### Define Plugin Entrypoint in pyproject.toml Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/extending.md This TOML snippet shows how to define a plugin entrypoint for Narwhals under the 'narwhals.plugins' section in your project's pyproject.toml file. Replace '' with the actual name of your library. ```toml [project.entry-points.'narwhals.plugins'] narwhals- = 'narwhals_' ``` -------------------------------- ### Get Categories Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/api-reference/expr_cat.md Retrieves the categories associated with a given expression. ```APIDOC ## Expr.cat.get_categories ### Description Retrieves the categories associated with a given expression. ### Method (Not specified, likely a method call on an expression object) ### Parameters (Parameters not explicitly detailed in the source, but would typically involve an expression object) ### Request Example (Not specified) ### Response (Response details not specified, but would likely be a list or representation of categories) ``` -------------------------------- ### Navigate to Repository Directory Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Change the current directory to the newly cloned Narwhals repository folder. ```bash cd narwhals-dev ``` -------------------------------- ### Implement Plugin Functions in __init__.py Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/extending.md This Python code demonstrates the essential functions and constants required in a plugin's top-level __init__.py file. It includes `is_native` to check for native dataframes, `__narwhals_namespace__` to provide a compliant namespace, and `NATIVE_PACKAGE` to specify the target library. ```python from narwhals.plugins import Plugin NATIVE_PACKAGE = "" def is_native(obj) -> bool: # Implementation to check if obj is a dataframe of the library ... def __narwhals_namespace__(version: str) -> Plugin: # Implementation to return a compliant namespace ... ``` -------------------------------- ### Update uv Package Manager Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Keeps the uv package and environment manager up-to-date. Ensure uv is installed before running this command. ```bash uv self update ``` -------------------------------- ### Testing Group-by with Polars Eager DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet demonstrates using the group-by `func` with an eager Polars DataFrame. Ensure Polars is installed and imported. ```python import polars as pl df = pl.DataFrame({"a": [1, 1, 2], "b": [4, 5, 6]}) print(func(df)) ``` -------------------------------- ### Run Unit Tests and Doctests Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute both unit tests and doctests simultaneously, along with a coverage report. This command ensures comprehensive testing of the codebase. ```bash uv run pytest src tests --cov=src --doctest-modules ``` -------------------------------- ### Run Tests for Specific Constructors Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute tests for a curated list of backend constructors. Use the `--constructors` flag to specify which backends to include in the test run. ```bash uv run pytest --constructors="pandas,pyarrow,polars[eager]" ``` -------------------------------- ### Conditional DataFrame Transformation Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/backcompat.md Example of handling API changes in different library versions using version parsing. This pattern is discouraged in favor of Narwhals' stable APIs. ```python if parse_version(pdx.__version__) < parse_version("1.3.0"): df = df.brewbeer() elif parse_version("1.3.0") <= parse_version(pdx.__version__) < parse_version("1.5.0"): df = df.brew_beer() else: df = df.brew_drink("beer") ``` -------------------------------- ### Testing with Polars Lazy DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet shows how to use the `func` with a lazy Polars DataFrame. Note the use of `.collect()` to trigger computation. Ensure Polars is installed and imported. ```python import polars as pl df = pl.LazyFrame({"a": [1, 1, 2]}) print(func(df).collect()) ``` -------------------------------- ### Running StandardScaler with Pandas DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/complete_example.md Demonstrates how to instantiate and use the StandardScaler with a Pandas DataFrame. The `fit` method processes the training data, and `transform` scales the test data. ```python import pandas as pd df_train = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 7]}) df_test = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 7]}) scaler = StandardScaler() scaler.fit(df_train) print(scaler.transform(df_test)) ``` -------------------------------- ### Run Tests for All CPU Constructors Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute tests for all available CPU constructors, including additional dependencies like Modin and PySpark. The `--all-cpu-constructors` flag enables this mode. ```bash uv run --extra modin --extra pyspark pytest --all-cpu-constructors ``` -------------------------------- ### Import Narwhals for Prototyping Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/backcompat.md Use this import for rapid iteration during development. It allows for quick changes without strict versioning concerns. ```python import narwhals as nw ``` -------------------------------- ### Filter DataFrame based on Series with `@nw.narwhalify` Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md Use `@nw.narwhalify(eager_only=True)` to decorate functions that filter DataFrames based on Series values. This example calculates the number of rows remaining after filtering. ```python import narwhals as nw from narwhals.typing import DataFrameT @nw.narwhalify(eager_only=True) def func(df: DataFrameT, s: nw.Series, col_name: str) -> int: return df.filter(nw.col(col_name).is_in(s)).shape[0] ``` -------------------------------- ### Run Command with uv Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute a command within the project's virtual environment using `uv run`. This ensures the environment is up-to-date. ```bash uv run pytest ``` -------------------------------- ### Testing Group-by with Polars Lazy DataFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md This snippet shows how to use the group-by `func` with a lazy Polars DataFrame. Note the use of `.collect()` to trigger computation. Ensure Polars is installed and imported. ```python import polars as pl df = pl.LazyFrame({"a": [1, 1, 2], "b": [4, 5, 6]}) print(func(df).collect()) ``` -------------------------------- ### Compute Horizontal Sum with `nw.sum_horizontal` Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/dataframe.md Use `nw.sum_horizontal` to compute the sum of specified columns. This example shows how to integrate it within a function decorated with `@nw.narwhalify` for seamless use with different DataFrame backends. ```python import narwhals as nw from narwhals.typing import IntoFrameT def func(df: IntoFrameT) -> IntoFrameT: return ( nw.from_native(df) .with_columns(a_plus_b=nw.sum_horizontal("a", "b")) .to_native() ) ``` ```python import narwhals as nw from narwhals.typing import FrameT @nw.narwhalify def func(df: FrameT) -> FrameT: return df.with_columns(a_plus_b=nw.sum_horizontal("a", "b")) ``` -------------------------------- ### Run All Queries for a Specific Backend Source: https://github.com/narwhals-dev/narwhals/blob/main/tpch/README.md Execute all TPC-H queries for a given backend, such as Polars. This is useful for testing a specific environment. ```bash pytest -k "polars" ``` -------------------------------- ### Run a Specific Query for All Backends Source: https://github.com/narwhals-dev/narwhals/blob/main/tpch/README.md Execute a single TPC-H query (e.g., q1) across all supported backends. The trailing dash prevents matching queries like q11 or q12. ```bash pytest -k "q1-" ``` -------------------------------- ### Sync with Extra Dependencies Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Synchronize the project dependencies, including the 'local-dev' group and additional extras like Dask and PySpark. ```bash uv sync --group local-dev --extra dask --extra pyspark ``` -------------------------------- ### Generate SQL via SQLFrame Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/generating_sql.md This snippet demonstrates generating SQL using SQLFrame. It sets up a `StandaloneSession`, registers a 'prices' table, and then uses the `avg_monthly_price` function with SQLFrame's DataFrame API. ```python from sqlframe.standalone import StandaloneSession session = StandaloneSession.builder.getOrCreate() session.catalog.add_table("prices", column_mapping={"date": "date", "price": "float"}) df = nw.from_native(session.read.table("prices")) print(avg_monthly_price(df).to_native().sql(dialect="duckdb")) ``` -------------------------------- ### Import Narwhals for Production Stability (v1) Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/backcompat.md Use this import if your project already relies on narwhals.stable.v1 and does not require newer features. No need to switch if v1 meets your needs. ```python import narwhals.stable.v1 as nw ``` -------------------------------- ### Fit Method: StandardScaler using from/to_native Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/complete_example.md Implement the `fit` method to calculate means and standard deviations. Use `nw.from_native(..., eager_only=True)` to ensure eager evaluation for Series operations. ```python from typing import Self import narwhals as nw from narwhals.typing import IntoDataFrameT class StandardScaler: def fit(self, df: IntoDataFrameT) -> Self: df_nw = nw.from_native(df, eager_only=True) self._means = {col: df_nw[col].mean() for col in df_nw.columns} self._std_devs = {col: df_nw[col].std() for col in df_nw.columns} self._columns = df_nw.columns return self ``` -------------------------------- ### Run a Specific Query for a Specific Backend Source: https://github.com/narwhals-dev/narwhals/blob/main/tpch/README.md Execute a single TPC-H query (e.g., q1) for a specific backend, including its associated extras like the PyArrow engine for Pandas. ```bash pytest -k "q1-pandas[pyarrow]" ``` -------------------------------- ### Verify Git Remotes Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Check the configured Git remotes to ensure both 'origin' and 'upstream' are correctly set up. ```bash git remote -v ``` -------------------------------- ### Run Static Typing Checks Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute the static typing checks for the project, which is a separate process from the pre-commit hooks. ```bash make typing ``` -------------------------------- ### Initialize Narwhals DataFrame and Select Column Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Demonstrates initializing a Narwhals DataFrame from a pandas DataFrame and performing a select operation with an arithmetic expression. This sets up the context for API mapping. ```python import narwhals as nw from narwhals._pandas_like.namespace import PandasLikeNamespace from narwhals._pandas_like.utils import Implementation from narwhals._pandas_like.dataframe import PandasLikeDataFrame from narwhals._utils import parse_version, Version import pandas as pd pn = PandasLikeNamespace( implementation=Implementation.PANDAS, version=Version.MAIN, ) df_pd = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) df = nw.from_native(df_pd) df.select(nw.col("a") + 1) ``` -------------------------------- ### Type Hint Preservation with nw.from_native Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/api-reference/typing.md Demonstrates how `nw.from_native` preserves type hints when converting native DataFrames. Use this method for better type inference with static analysis tools like mypy. ```python import polars as pl import narwhals as nw from narwhals.typing import IntoDataFrameT, DataFrameT df = pl.DataFrame({"a": [1, 2, 3]}) def func(df_native: IntoDataFrameT) -> IntoDataFrameT: df = nw.from_native(df_native, eager_only=True) return df.select(b=nw.col("a")).to_native() reveal_type(func(df)) ``` -------------------------------- ### DataFrame Transformation with Narwhals Stable API (from/to_native) Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/backcompat.md Demonstrates using narwhals.stable.v2 to perform a DataFrame transformation, ensuring compatibility even if underlying backend APIs change. Imports `narwhals.stable.v2` as `nw` and uses `nw.from_native` and `nw.to_native`. ```python import narwhals.stable.v2 as nw from narwhals.stable.v2.typing import IntoFrameT def func(df: IntoFrameT) -> IntoFrameT: return nw.from_native(df).with_columns(nw.col("a").cum_sum()).to_native() ``` -------------------------------- ### Left Join Column Names (Pandas vs. Polars) Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/why.md Illustrates how pandas and Polars differ in naming columns after a left join when the join keys have different names. ```python pd_df_left = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) pd_df_right = pd.DataFrame({"a": [1, 2, 3], "c": [4, 5, 6]}) pd_left_merge = pd_df_left.merge(pd_df_right, left_on="b", right_on="c", how="left") pl_df_left = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) pl_df_right = pl.DataFrame({"a": [1, 2, 3], "c": [4, 5, 6]}) pl_left_merge = pl_df_left.join(pl_df_right, left_on="b", right_on="c", how="left") print(pd_left_merge.columns) print(pl_left_merge.columns) ``` -------------------------------- ### Add Upstream Remote and Fetch Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Add the official Narwhals repository as an 'upstream' remote and fetch its latest changes. ```bash git remote add upstream git@github.com:narwhals-dev/narwhals.git git fetch upstream ``` -------------------------------- ### Add Optional Dependency Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Use `uv add --optional` to add a package to an optional extra, like `dask`. This is useful for managing dependencies that are not always required. ```terminal uv add --optional dask ``` -------------------------------- ### Run Tests with cudf.pandas Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Execute tests specifically using the cudf.pandas backend. This requires setting the NARWHALS_DEFAULT_CONSTRUCTORS environment variable and including the cudf and cudf.pandas modules. ```bash NARWHALS_DEFAULT_CONSTRUCTORS=pandas uv run --extra cudf --module cudf.pandas --module pytest ``` -------------------------------- ### Check Null/NaN Behavior in Pandas Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/concepts/null_handling.md Demonstrates null and NaN handling in a standard pandas DataFrame. ```python import pandas as pd df = pd.DataFrame(data) print(check_null_behavior(df)) ``` -------------------------------- ### Translate nw.col('a', 'b') to Pandas Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Illustrates the pandas translation for selecting multiple columns. ```python def col_a_b(df): return [df.loc[:, "a"], df.loc[:, "b"]] ``` -------------------------------- ### Translate nw.col('a') to Pandas Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/how_it_works.md Demonstrates the pandas equivalent of selecting a single column expression. ```python def col_a(df): return [df.loc[:, "a"]] ``` -------------------------------- ### Implementation Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/api-reference/utils.md Represents an implementation of the Narwhals API. ```APIDOC ## Class `narwhals.utils.Implementation` ### Description Represents an implementation of the Narwhals API. ### Methods #### `__init__` Initializes the Implementation object. #### `name` Returns the name of the implementation. ``` -------------------------------- ### Create New Release Branch Source: https://github.com/narwhals-dev/narwhals/wiki/Home Create a new branch named 'bump-version' to isolate changes for version bumping. ```bash git checkout -b bump-version ``` -------------------------------- ### Clone Narwhals Repository Source: https://github.com/narwhals-dev/narwhals/blob/main/CONTRIBUTING.md Clone the forked Narwhals repository to your local machine using the copied URL. ```bash git clone ``` -------------------------------- ### Import Narwhals for Production Stability (v2) Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/backcompat.md Switch to this import for production-ready code to ensure stability. Requires narwhals>=2.0. ```python import narwhals.stable.v2 as nw ``` -------------------------------- ### Add Narwhals using uv Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/installation.md Use this command to add Narwhals to a new project managed by uv. It creates a virtual environment, adds narwhals to pyproject.toml, and updates uv.lock. ```bash uv add narwhals ``` -------------------------------- ### NaN Comparisons in Python and PyArrow Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/concepts/null_handling.md Demonstrates NaN comparison behavior in standard Python and PyArrow, adhering to the IEEE-754 standard where NaN is not equal to itself. ```python import pyarrow as pa import pyarrow.compute as pc print("Python result:") print(float("nan") == float("nan"), 0.0 == 0.0) print() print("PyArrow result:") arr = pa.array([float("nan"), 0.0]) print(pc.equal(arr, arr)) ``` -------------------------------- ### Fit Method: StandardScaler using @narwhalify Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/complete_example.md Implement the `fit` method using the `@nw.narwhalify(eager_only=True)` decorator. This decorator propagates `eager_only=True` to `nw.from_native` for eager evaluation. ```python from typing import Self import narwhals as nw from narwhals.typing import DataFrameT class StandardScaler: @nw.narwhalify(eager_only=True) def fit(self, df: DataFrameT) -> Self: self._means = {col: df[col].mean() for col in df.columns} self._std_devs = {col: df[col].std() for col in df.columns} self._columns = df.columns return self ``` -------------------------------- ### Check Null/NaN Behavior in PyArrow Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/concepts/null_handling.md Examines null and NaN handling using PyArrow, demonstrating its clear separation of these concepts. ```python import pyarrow as pa df = pa.table(data) print(check_null_behavior(df)) ``` -------------------------------- ### StandardScaler using from/to_native Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/basics/complete_example.md Implements a StandardScaler using `narwhals.from_native` and `to_native` for converting between native DataFrames and Narwhals DataFrames. This approach ensures compatibility with various DataFrame libraries. ```python from typing import Self import narwhals as nw from narwhals.typing import IntoDataFrameT from narwhals.typing import IntoFrameT class StandardScaler: def fit(self, df: IntoDataFrameT) -> Self: df_nw = nw.from_native(df, eager_only=True) self._means = {col: df_nw[col].mean() for col in df_nw.columns} self._std_devs = {col: df_nw[col].std() for col in df_nw.columns} self._columns = df_nw.columns return self def transform(self, df: IntoFrameT) -> IntoFrameT: df_nw = nw.from_native(df) return df_nw.with_columns( (nw.col(col) - self._means[col]) / self._std_devs[col] for col in self._columns ).to_native() ``` -------------------------------- ### Series Methods and Properties Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/api-reference/series.md This section lists the publicly available methods and properties of the `narwhals.Series` object that users can directly invoke. ```APIDOC ## Series API This document outlines the methods and properties available on the `narwhals.Series` object. ### Methods - `__arrow_c_stream__()`: Returns a PyArrow stream. - `__getitem__(key)`: Access elements by index or slice. - `__iter__()`: Returns an iterator for the Series. - `abs()`: Computes the element-wise absolute value. - `alias(name)`: Renames the Series. - `all()`: Checks if all elements are truthy. - `any()`: Checks if any element is truthy. - `any_value()`: Returns any single value from the Series. - `arg_max()`: Returns the index of the maximum value. - `arg_min()`: Returns the index of the minimum value. - `arg_true()`: Returns the index of the first true value. - `cast(dtype)`: Casts the Series to a different data type. - `ceil()`: Computes the element-wise ceiling. - `clip(lower, upper)`: Clips values to a specified range. - `cos()`: Computes the element-wise cosine. - `count()`: Counts the number of non-null elements. - `cum_count()`: Computes the cumulative count. - `cum_max()`: Computes the cumulative maximum. - `cum_min()`: Computes the cumulative minimum. - `cum_prod()`: Computes the cumulative product. - `cum_sum()`: Computes the cumulative sum. - `diff()`: Computes the difference between consecutive elements. - `drop_nulls()`: Returns a Series with null values removed. - `ewm_mean()`: Computes the Exponentially Weighted Mean. - `exp()`: Computes the element-wise exponential. - `fill_nan(value)`: Fills NaN values with a specified value. - `fill_null(value)`: Fills null values with a specified value. - `filter(predicate)`: Filters elements based on a predicate. - `first()`: Returns the first element. - `floor()`: Computes the element-wise floor. - `from_iterable(iterable, dtype)`: Creates a Series from an iterable. - `from_numpy(array)`: Creates a Series from a NumPy array. - `gather_every(step)`: Selects elements at regular intervals. - `head(n)`: Returns the first n elements. - `hist()`: Computes the histogram of the Series. - `is_between(lower, upper)`: Checks if elements are within a range. - `is_close(other, rel_tol, abs_tol)`: Checks if elements are close to another Series. - `is_duplicated()`: Checks for duplicated elements. - `is_empty()`: Checks if the Series is empty. - `is_finite()`: Checks if elements are finite. - `is_first_distinct()`: Checks if an element is the first occurrence of its value. - `is_in(values)`: Checks if elements are present in a given set of values. - `is_last_distinct()`: Checks if an element is the last occurrence of its value. - `is_nan()`: Checks for NaN values. - `is_null()`: Checks for null values. - `is_sorted()`: Checks if the Series is sorted. - `is_unique()`: Checks for unique elements. - `item()`: Returns the single element of a Series with one element. - `kurtosis()`: Computes the kurtosis. - `last()`: Returns the last element. - `len()`: Returns the number of elements. - `log()`: Computes the element-wise natural logarithm. - `max()`: Computes the maximum value. - `mean()`: Computes the mean value. - `median()`: Computes the median value. - `min()`: Computes the minimum value. - `mode()`: Computes the mode(s) of the Series. - `n_unique()`: Counts the number of unique elements. - `null_count()`: Counts the number of null elements. - `pipe(func, *args, **kwargs)`: Applies a function to the Series. - `quantile(quantile)`: Computes the quantile. - `rank()`: Computes the rank of elements. - `rename(name)`: Renames the Series. - `replace_strict(old, new)`: Replaces values strictly. - `rolling_mean(window_size)`: Computes the rolling mean. - `rolling_std(window_size)`: Computes the rolling standard deviation. - `rolling_sum(window_size)`: Computes the rolling sum. - `rolling_var(window_size)`: Computes the rolling variance. - `round(decimals)`: Rounds elements to a specified number of decimals. - `sample(n)`: Returns a random sample of elements. - `scatter(other)`: Creates a scatter plot with another Series. - `shape()`: Returns the shape of the Series. - `shift(periods)`: Shifts elements by a specified number of periods. - `sin()`: Computes the element-wise sine. - `skew()`: Computes the skewness. - `sort(descending)`: Sorts the Series. - `sqrt()`: Computes the element-wise square root. - `std()`: Computes the standard deviation. - `sum()`: Computes the sum of elements. - `tail(n)`: Returns the last n elements. - `to_arrow()`: Converts the Series to a PyArrow array. - `to_dummies(drop_first)`: Converts categorical data to dummy variables. - `to_frame()`: Converts the Series to a DataFrame. - `to_list()`: Converts the Series to a Python list. - `to_native()`: Converts the Series to its native representation. - `to_numpy()`: Converts the Series to a NumPy array. - `to_pandas()`: Converts the Series to a Pandas Series. - `to_polars()`: Converts the Series to a Polars Series. - `unique()`: Returns unique elements. - `value_counts()`: Computes the frequency of each unique value. - `var()`: Computes the variance. - `zip_with(other, f)`: Zips the Series with another Series using a function. ### Properties - `dtype`: Returns the data type of the Series. - `implementation`: Returns the underlying implementation of the Series. - `name`: Returns the name of the Series. ``` -------------------------------- ### Creating DataFrame with Integer Column Name Source: https://github.com/narwhals-dev/narwhals/blob/main/docs/concepts/column_names.md Demonstrates how pandas allows the creation of a DataFrame with integer column names, which Narwhals generally supports but may restrict in certain operations. ```python import pandas as pd df = pd.concat([pd.Series([1, 2], name=0), pd.Series([1, 3], name=0)], axis=1) print(df) ```