-
Notifications
You must be signed in to change notification settings - Fork 119
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 #26 from MarcoGorelli/str
Str namespace
- Loading branch information
Showing
5 changed files
with
94 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
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_lazy = pl.LazyFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) | ||
|
||
|
||
def test_group_by_complex() -> None: | ||
df = nw.LazyFrame(df_pandas) | ||
with pytest.warns(UserWarning, match="complex group-by"): | ||
result = nw.to_native( | ||
df.group_by("a").agg((nw.col("b") - nw.col("z").mean()).mean()).sort("a") | ||
) | ||
expected = {"a": [1, 2, 3], "b": [-3.0, -3.0, -4.0]} | ||
compare_dicts(result, expected) | ||
|
||
df = nw.LazyFrame(df_lazy) | ||
result = nw.to_native( | ||
df.group_by("a").agg((nw.col("b") - nw.col("z").mean()).mean()).sort("a") | ||
) | ||
expected = {"a": [1, 2, 3], "b": [-3.0, -3.0, -4.0]} | ||
compare_dicts(result, expected) | ||
|
||
|
||
def test_invalid_group_by() -> None: | ||
df = nw.LazyFrame(df_pandas) | ||
with pytest.raises(RuntimeError, match="does your"): | ||
df.group_by("a").agg(nw.col("b")) | ||
with pytest.raises( | ||
ValueError, match=r"Anonymous expressions are not supported in group_by\.agg" | ||
): | ||
df.group_by("a").agg(nw.all().mean()) |
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,38 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import warnings | ||
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": ["fdas", "edfas"]}) | ||
df_polars = pl.LazyFrame({"a": ["fdas", "edfas"]}) | ||
|
||
if os.environ.get("CI", None): | ||
import modin.pandas as mpd | ||
|
||
with warnings.catch_warnings(): | ||
warnings.filterwarnings("ignore", category=UserWarning) | ||
df_mpd = mpd.DataFrame({"a": ["fdas", "edfas"]}) | ||
else: | ||
df_mpd = df_pandas.copy() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df_raw", | ||
[df_pandas, df_polars, df_mpd], | ||
) | ||
def test_ends_with(df_raw: Any) -> None: | ||
df = nw.LazyFrame(df_raw) | ||
result = df.select(nw.col("a").str.ends_with("das")) | ||
result_native = nw.to_native(result) | ||
expected = { | ||
"a": [True, False], | ||
} | ||
compare_dicts(result_native, expected) |
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