-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add to_py_scalar * fix tests * more fixes pragma and doctsting * fix test_to_py_scalar_cudf_series * convert numpy scalars * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove parse version * simplify test_to_py_scalar_arrays_series * add conversion for datetime and timedelta * stricter to_py_scalar --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
59aa483
commit 0c1650c
Showing
6 changed files
with
162 additions
and
0 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
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,63 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from datetime import timedelta | ||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.dependencies import get_cudf | ||
|
||
if TYPE_CHECKING: | ||
from tests.utils import ConstructorEager | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input_value", "expected"), | ||
[ | ||
(1, 1), | ||
(1.0, 1.0), | ||
("a", "a"), | ||
(True, True), | ||
(b"a", b"a"), | ||
(datetime(2021, 1, 1), datetime(2021, 1, 1)), | ||
(timedelta(days=1), timedelta(days=1)), | ||
], | ||
) | ||
def test_to_py_scalar( | ||
constructor_eager: ConstructorEager, input_value: Any, expected: Any | ||
) -> None: | ||
df = nw.from_native(constructor_eager({"a": [input_value]})) | ||
output = nw.to_py_scalar(df["a"].item(0)) | ||
if expected == 1 and constructor_eager.__name__.startswith("pandas"): | ||
assert not isinstance(output, np.int64) | ||
elif isinstance(expected, datetime) and constructor_eager.__name__.startswith( | ||
"pandas" | ||
): | ||
assert not isinstance(output, pd.Timestamp) | ||
elif isinstance(expected, timedelta) and constructor_eager.__name__.startswith( | ||
"pandas" | ||
): | ||
assert not isinstance(output, pd.Timedelta) | ||
assert output == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_value", | ||
[np.array([1, 2]), [1, 2, 3], {"a": [1, 2, 3]}], | ||
) | ||
def test_to_py_scalar_value_error(input_value: Any) -> None: | ||
with pytest.raises(ValueError, match="Expected object convertible to a scalar"): | ||
nw.to_py_scalar(input_value) | ||
|
||
|
||
def test_to_py_scalar_value_error_cudf() -> None: | ||
if cudf := get_cudf(): # pragma: no cover | ||
df = nw.from_native(cudf.DataFrame({"a": [1, 2, 3]})) | ||
|
||
with pytest.raises(ValueError, match="Expected object convertible to a scalar"): | ||
nw.to_py_scalar(df["a"]) |