From 93b0e7ca176a212785108437478375d4086bea81 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:20:24 +0100 Subject: [PATCH 1/6] extra ci --- .github/workflows/extremes.yml | 67 +++++++++++++++ tests/test_series.py | 153 +++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 .github/workflows/extremes.yml create mode 100644 tests/test_series.py diff --git a/.github/workflows/extremes.yml b/.github/workflows/extremes.yml new file mode 100644 index 000000000..02e1073f2 --- /dev/null +++ b/.github/workflows/extremes.yml @@ -0,0 +1,67 @@ +name: ci + +on: + pull_request: + push: + branches: [main] + +jobs: + minimum_versions: + strategy: + matrix: + python-version: ["3.8"] + os: [ubuntu-latest] + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Cache multiple paths + uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + $RUNNER_TOOL_CACHE/Python/* + ~\AppData\Local\pip\Cache + key: ${{ runner.os }}-build-${{ matrix.python-version }} + - name: install-reqs + run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt + - name: install-modin + run: python -m pip install pandas==2.0.0 polars==0.20.0 + - name: Run pytest + run: pytest tests --cov=narwhals --cov=tests + - name: Run doctests + run: pytest narwhals --doctest-modules + + nightlies: + strategy: + matrix: + python-version: ["3.12"] + os: [ubuntu-latest] + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Cache multiple paths + uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + $RUNNER_TOOL_CACHE/Python/* + ~\AppData\Local\pip\Cache + key: ${{ runner.os }}-build-${{ matrix.python-version }} + - name: install-reqs + run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt + - name: uninstall pandas + run: python -m pip uninstall pandas -y + - name: install-nightly + run: python -m pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple pandas + - name: Run pytest + run: pytest tests --cov=narwhals --cov=tests + - name: Run doctests + run: pytest narwhals --doctest-modules diff --git a/tests/test_series.py b/tests/test_series.py new file mode 100644 index 000000000..18c4c844d --- /dev/null +++ b/tests/test_series.py @@ -0,0 +1,153 @@ +from __future__ import annotations + +from typing import Any + +import pandas as pd +import polars as pl +import pytest + +import narwhals as nw + +df_pandas = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) +df_polars = pl.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) +df_lazy = pl.LazyFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) + + +@pytest.mark.parametrize("df_raw", [df_pandas, df_lazy]) +def test_len(df_raw: Any) -> None: + result = len(nw.LazyFrame(df_raw).collect()["a"]) + assert result == 3 + result = len(nw.to_native(nw.LazyFrame(df_raw).collect()["a"])) + assert result == 3 + + +@pytest.mark.parametrize("df_raw", [df_pandas, df_lazy]) +def test_dtype(df_raw: Any) -> None: + result = nw.LazyFrame(df_raw).collect()["a"].dtype + assert result == nw.Int64 + assert result.is_numeric() + + +def test_dtypes() -> None: + df = pl.DataFrame( + { + "a": [1], + "b": [1], + "c": [1], + "d": [1], + "e": [1], + "f": [1], + "g": [1], + "h": [1], + "i": [1], + "j": [1], + "k": [1], + "l": [1], + "m": [True], + }, + schema={ + "a": pl.Int64, + "b": pl.Int32, + "c": pl.Int16, + "d": pl.Int8, + "e": pl.UInt64, + "f": pl.UInt32, + "g": pl.UInt16, + "h": pl.UInt8, + "i": pl.Float64, + "j": pl.Float32, + "k": pl.String, + "l": pl.Datetime, + "m": pl.Boolean, + }, + ) + result = nw.DataFrame(df).schema + expected = { + "a": nw.Int64, + "b": nw.Int32, + "c": nw.Int16, + "d": nw.Int8, + "e": nw.UInt64, + "f": nw.UInt32, + "g": nw.UInt16, + "h": nw.UInt8, + "i": nw.Float64, + "j": nw.Float32, + "k": nw.String, + "l": nw.Datetime, + "m": nw.Boolean, + } + assert result == expected + result_pd = nw.DataFrame(df.to_pandas()).schema + assert result_pd == expected + + +def test_cast() -> None: + df = pl.DataFrame( + { + "a": [1], + "b": [1], + "c": [1], + "d": [1], + "e": [1], + "f": [1], + "g": [1], + "h": [1], + "i": [1], + "j": [1], + "k": [1], + "l": [1], + "m": [True], + }, + schema={ + "a": pl.Int64, + "b": pl.Int32, + "c": pl.Int16, + "d": pl.Int8, + "e": pl.UInt64, + "f": pl.UInt32, + "g": pl.UInt16, + "h": pl.UInt8, + "i": pl.Float64, + "j": pl.Float32, + "k": pl.String, + "l": pl.Datetime, + "m": pl.Boolean, + }, + ) + df = nw.DataFrame(df).select( # type: ignore[assignment] + nw.col("a").cast(nw.Int32), + nw.col("b").cast(nw.Int16), + nw.col("c").cast(nw.Int8), + nw.col("d").cast(nw.Int64), + nw.col("e").cast(nw.UInt32), + nw.col("f").cast(nw.UInt16), + nw.col("g").cast(nw.UInt8), + nw.col("h").cast(nw.UInt64), + nw.col("i").cast(nw.Float32), + nw.col("j").cast(nw.Float64), + nw.col("k").cast(nw.String), + nw.col("l").cast(nw.Datetime), + nw.col("m").cast(nw.Int8), + n=nw.col("m").cast(nw.Boolean), + ) + result = df.schema + expected = { + "a": nw.Int32, + "b": nw.Int16, + "c": nw.Int8, + "d": nw.Int64, + "e": nw.UInt32, + "f": nw.UInt16, + "g": nw.UInt8, + "h": nw.UInt64, + "i": nw.Float32, + "j": nw.Float64, + "k": nw.String, + "l": nw.Datetime, + "m": nw.Int8, + "n": nw.Boolean, + } + assert result == expected + result_pd = nw.from_native(df.to_pandas()).schema + assert result_pd == expected From 711bc3510094259e8cdb45ed9125a44324d5ee23 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:22:59 +0100 Subject: [PATCH 2/6] extra ci --- .github/workflows/extremes.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/extremes.yml b/.github/workflows/extremes.yml index 02e1073f2..02e2ecc08 100644 --- a/.github/workflows/extremes.yml +++ b/.github/workflows/extremes.yml @@ -59,6 +59,8 @@ jobs: run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt - name: uninstall pandas run: python -m pip uninstall pandas -y + - name: install-modin + run: pip install modin[dask] - name: install-nightly run: python -m pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple pandas - name: Run pytest From bb243beaf70c2e2cf00b278cd114bf1ef14f69a4 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:23:19 +0100 Subject: [PATCH 3/6] extra ci --- .github/workflows/extremes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/extremes.yml b/.github/workflows/extremes.yml index 02e2ecc08..16bf4e240 100644 --- a/.github/workflows/extremes.yml +++ b/.github/workflows/extremes.yml @@ -29,7 +29,7 @@ jobs: - name: install-reqs run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt - name: install-modin - run: python -m pip install pandas==2.0.0 polars==0.20.0 + run: python -m pip install pandas==2.0.0 polars==0.20.0 modin[dask] - name: Run pytest run: pytest tests --cov=narwhals --cov=tests - name: Run doctests From ef0a9651f6d1e9cdf2e60a97849f76d0e0f7a4ca Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:26:28 +0100 Subject: [PATCH 4/6] extra ci --- .github/workflows/extremes.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/extremes.yml b/.github/workflows/extremes.yml index 16bf4e240..ef4ff9cae 100644 --- a/.github/workflows/extremes.yml +++ b/.github/workflows/extremes.yml @@ -29,9 +29,9 @@ jobs: - name: install-reqs run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt - name: install-modin - run: python -m pip install pandas==2.0.0 polars==0.20.0 modin[dask] + run: python -m pip install pandas==2.0.0 polars==0.20.4 modin[dask] - name: Run pytest - run: pytest tests --cov=narwhals --cov=tests + run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50 - name: Run doctests run: pytest narwhals --doctest-modules @@ -64,6 +64,6 @@ jobs: - name: install-nightly run: python -m pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple pandas - name: Run pytest - run: pytest tests --cov=narwhals --cov=tests + run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50 - name: Run doctests run: pytest narwhals --doctest-modules From 674f5ac65a9a9be811c85812abb3217aae6bbcca Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:29:33 +0100 Subject: [PATCH 5/6] extra ci --- .github/workflows/extremes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/extremes.yml b/.github/workflows/extremes.yml index ef4ff9cae..071f9e111 100644 --- a/.github/workflows/extremes.yml +++ b/.github/workflows/extremes.yml @@ -29,7 +29,7 @@ jobs: - name: install-reqs run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt - name: install-modin - run: python -m pip install pandas==2.0.0 polars==0.20.4 modin[dask] + run: python -m pip install pandas==2.0.0 polars==0.20.5 modin[dask] - name: Run pytest run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50 - name: Run doctests From ee9f23657112cee63b509c5462e268a02b5f9048 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:31:39 +0100 Subject: [PATCH 6/6] just accept dict in rename for now --- narwhals/dataframe.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index f60b9fa60..a70a49b21 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -435,8 +435,7 @@ def rename(self, mapping: dict[str, str]) -> Self: Rename column names. Arguments: - mapping: Key value pairs that map from old name to new name, or a function - that takes the old name as input and returns the new name. + mapping: Key value pairs that map from old name to new name. Examples: >>> import polars as pl @@ -462,23 +461,6 @@ def rename(self, mapping: dict[str, str]) -> Self: │ 2 ┆ 7 ┆ b │ │ 3 ┆ 8 ┆ c │ └───────┴─────┴─────┘ - >>> dframe = df.rename(lambda column_name: "f" + column_name[1:]) - >>> dframe - ┌─────────────────────────────────────────────────┐ - | Narwhals DataFrame | - | Use `narwhals.to_native()` to see native output | - └─────────────────────────────────────────────────┘ - >>> nw.to_native(dframe) - shape: (3, 3) - ┌─────┬─────┬─────┐ - │ foo ┆ far ┆ fam │ - │ --- ┆ --- ┆ --- │ - │ i64 ┆ i64 ┆ str │ - ╞═════╪═════╪═════╡ - │ 1 ┆ 6 ┆ a │ - │ 2 ┆ 7 ┆ b │ - │ 3 ┆ 8 ┆ c │ - └─────┴─────┴─────┘ """ return super().rename(mapping)