Skip to content

Commit

Permalink
Merge pull request #35 from MarcoGorelli/extra-ci
Browse files Browse the repository at this point in the history
extra ci
  • Loading branch information
MarcoGorelli authored Apr 1, 2024
2 parents 86eef57 + ee9f236 commit d121e60
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 19 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/extremes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.5 modin[dask]
- name: Run pytest
run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50
- 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-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
run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50
- name: Run doctests
run: pytest narwhals --doctest-modules
20 changes: 1 addition & 19 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
153 changes: 153 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d121e60

Please sign in to comment.