-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from raisadz/main
Add tests for common dataframe operations
- Loading branch information
Showing
5 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import pandas as pd | ||
import polars as pl | ||
import pytest | ||
|
||
import narwhals as nw | ||
from tests.utils import compare_dicts | ||
|
||
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]}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df_raw", | ||
[df_pandas, df_polars], | ||
) | ||
def test_sort(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.sort("a", "b") | ||
result_native = nw.to_native(result) | ||
expected = { | ||
"a": [1, 2, 3], | ||
"b": [4, 6, 4], | ||
"z": [7.0, 9.0, 8.0], | ||
} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df_raw", | ||
[df_pandas, df_polars], | ||
) | ||
def test_filter(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.filter(nw.col("a") > 1) | ||
result_native = nw.to_native(result) | ||
expected = {"a": [3, 2], "b": [4, 6], "z": [8.0, 9.0]} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df_raw", | ||
[df_pandas, df_polars], | ||
) | ||
def test_add(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.with_columns( | ||
c=nw.col("a") + nw.col("b"), | ||
d=nw.col("a") - nw.col("a").mean(), | ||
) | ||
result_native = nw.to_native(result) | ||
expected = { | ||
"a": [1, 3, 2], | ||
"b": [4, 4, 6], | ||
"z": [7.0, 8.0, 9.0], | ||
"c": [5, 7, 8], | ||
"d": [-1.0, 1.0, 0.0], | ||
} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df_raw", | ||
[df_pandas, df_polars], | ||
) | ||
def test_double(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.with_columns(nw.all() * 2) | ||
result_native = nw.to_native(result) | ||
expected = {"a": [2, 6, 4], "b": [8, 8, 12], "z": [14.0, 16.0, 18.0]} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars]) | ||
def test_sumh(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.with_columns(horizonal_sum=nw.sum_horizontal(nw.col("a"), nw.col("b"))) | ||
result_native = nw.to_native(result) | ||
expected = { | ||
"a": [1, 3, 2], | ||
"b": [4, 4, 6], | ||
"z": [7.0, 8.0, 9.0], | ||
"horizonal_sum": [5, 7, 8], | ||
} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars]) | ||
def test_sumh_literal(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.with_columns(horizonal_sum=nw.sum_horizontal("a", nw.col("b"))) | ||
result_native = nw.to_native(result) | ||
expected = { | ||
"a": [1, 3, 2], | ||
"b": [4, 4, 6], | ||
"z": [7.0, 8.0, 9.0], | ||
"horizonal_sum": [5, 7, 8], | ||
} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars]) | ||
def test_sum_all(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.select(nw.all().sum()) | ||
result_native = nw.to_native(result) | ||
expected = {"a": [6], "b": [14], "z": [24.0]} | ||
compare_dicts(result_native, expected) | ||
|
||
|
||
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars]) | ||
def test_double_selected(df_raw: Any) -> None: | ||
df = nw.DataFrame(df_raw) | ||
result = df.select(nw.col("a", "b") * 2) | ||
result_native = nw.to_native(result) | ||
expected = {"a": [2, 6, 4], "b": [8, 8, 12]} | ||
compare_dicts(result_native, expected) |