|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from collections import deque |
| 6 | +from datetime import datetime |
5 | 7 | from typing import TYPE_CHECKING, Any |
6 | 8 |
|
7 | 9 | import numpy as np |
|
10 | 12 |
|
11 | 13 | import narwhals.stable.v2 as nw_v2 |
12 | 14 | from narwhals.utils import Version |
13 | | -from tests.utils import PANDAS_VERSION, Constructor, assert_equal_data |
| 15 | +from tests.utils import ( |
| 16 | + PANDAS_VERSION, |
| 17 | + Constructor, |
| 18 | + assert_equal_data, |
| 19 | + assert_equal_series, |
| 20 | +) |
14 | 21 |
|
15 | 22 | if TYPE_CHECKING: |
| 23 | + from collections.abc import Sequence |
| 24 | + |
16 | 25 | from typing_extensions import assert_type |
17 | 26 |
|
| 27 | + from narwhals._namespace import EagerAllowed |
18 | 28 | from narwhals.stable.v2.typing import IntoDataFrameT |
| 29 | + from narwhals.typing import IntoDType |
19 | 30 |
|
20 | 31 |
|
21 | 32 | def test_toplevel() -> None: |
@@ -332,3 +343,30 @@ def test_imports() -> None: |
332 | 343 | from narwhals.stable.v2.dtypes import Enum # noqa: F401 |
333 | 344 | from narwhals.stable.v2.selectors import datetime # noqa: F401 |
334 | 345 | from narwhals.stable.v2.typing import IntoDataFrame # noqa: F401 |
| 346 | + |
| 347 | + |
| 348 | +@pytest.mark.parametrize( |
| 349 | + ("dtype", "expected"), |
| 350 | + [ |
| 351 | + (None, [5, 2, 0, 1]), |
| 352 | + (nw_v2.Int64, [5, 2, 0, 1]), |
| 353 | + (nw_v2.String, ("a", "b", "c")), |
| 354 | + (nw_v2.Float64, [5.0, 2.0, 0.0, 1.0]), |
| 355 | + ( |
| 356 | + nw_v2.Datetime(), |
| 357 | + deque([datetime(2005, 1, 1, 10), datetime(2002, 1, 1, 10, 43)]), |
| 358 | + ), |
| 359 | + ], |
| 360 | + ids=str, |
| 361 | +) |
| 362 | +def test_series_from_iterable( |
| 363 | + eager_backend: EagerAllowed, dtype: IntoDType | None, expected: Sequence[Any] |
| 364 | +) -> None: |
| 365 | + data = expected |
| 366 | + name = "abc" |
| 367 | + result = nw_v2.Series.from_iterable(name, data, backend=eager_backend, dtype=dtype) |
| 368 | + assert result._version is Version.V2 |
| 369 | + assert isinstance(result, nw_v2.Series) |
| 370 | + if dtype: |
| 371 | + assert result.dtype == dtype |
| 372 | + assert_equal_series(result, expected, name) |
0 commit comments