Skip to content

Commit 1feb8cc

Browse files
committed
feat: Add PolarsSeries.from_iterable
1 parent 3b50935 commit 1feb8cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

narwhals/_polars/series.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import TYPE_CHECKING
44
from typing import Any
5+
from typing import Iterable
56
from typing import Sequence
67
from typing import cast
78
from typing import overload
@@ -74,6 +75,28 @@ def _change_version(self: Self, version: Version) -> Self:
7475
self.native, backend_version=self._backend_version, version=version
7576
)
7677

78+
@classmethod
79+
def from_iterable(
80+
cls,
81+
data: Iterable[Any],
82+
*,
83+
context: _FullContext,
84+
name: str = "",
85+
dtype: DType | type[DType] | None = None,
86+
) -> Self:
87+
version = context._version
88+
backend_version = context._backend_version
89+
dtype_pl = (
90+
narwhals_to_native_dtype(dtype, version, backend_version) if dtype else None
91+
)
92+
# NOTE: `Iterable` is fine, annotation is overly narrow
93+
# https://github.com/pola-rs/polars/blob/82d57a4ee41f87c11ca1b1af15488459727efdd7/py-polars/polars/series/series.py#L332-L333
94+
return cls(
95+
pl.Series(name=name, values=cast("Sequence[Any]", data), dtype=dtype_pl),
96+
backend_version=backend_version,
97+
version=version,
98+
)
99+
77100
@classmethod
78101
def from_numpy(cls, data: Into1DArray, /, *, context: _FullContext) -> Self:
79102
return cls(

0 commit comments

Comments
 (0)