Skip to content

Commit cc73797

Browse files
committed
refactor: Simplify nw.new_series
Follow up to #2283 (comment)
1 parent 9f8c7a6 commit cc73797

File tree

1 file changed

+5
-41
lines changed

1 file changed

+5
-41
lines changed

narwhals/functions.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
if TYPE_CHECKING:
4545
from types import ModuleType
4646

47-
import pyarrow as pa
4847
from typing_extensions import Self
4948
from typing_extensions import TypeAlias
5049
from typing_extensions import TypeIs
@@ -260,50 +259,15 @@ def _new_series_impl(
260259
version: Version,
261260
) -> Series[Any]:
262261
implementation = Implementation.from_backend(backend)
263-
native_namespace = implementation.to_native_namespace()
264-
265-
if implementation is Implementation.POLARS:
266-
if dtype:
267-
from narwhals._polars.utils import (
268-
narwhals_to_native_dtype as polars_narwhals_to_native_dtype,
269-
)
270-
271-
backend_version = parse_version(native_namespace.__version__)
272-
dtype_pl = polars_narwhals_to_native_dtype(
273-
dtype, version=version, backend_version=backend_version
274-
)
275-
else:
276-
dtype_pl = None
277-
278-
native_series = native_namespace.Series(name=name, values=values, dtype=dtype_pl)
279-
elif implementation.is_pandas_like():
280-
if dtype:
281-
from narwhals._pandas_like.utils import (
282-
narwhals_to_native_dtype as pandas_like_narwhals_to_native_dtype,
283-
)
284-
285-
backend_version = parse_version(native_namespace)
286-
pd_dtype = pandas_like_narwhals_to_native_dtype(
287-
dtype, None, implementation, backend_version, version
288-
)
289-
native_series = native_namespace.Series(values, name=name, dtype=pd_dtype)
290-
else:
291-
native_series = native_namespace.Series(values, name=name)
292-
293-
elif implementation is Implementation.PYARROW:
294-
pa_dtype: pa.DataType | None = None
295-
if dtype:
296-
from narwhals._arrow.utils import (
297-
narwhals_to_native_dtype as arrow_narwhals_to_native_dtype,
298-
)
299-
300-
pa_dtype = arrow_narwhals_to_native_dtype(dtype, version=version)
301-
native_series = native_namespace.chunked_array([values], type=pa_dtype)
302-
262+
if is_eager_allowed(implementation):
263+
ns = _into_compliant_namespace(implementation, version)
264+
series = ns._series.from_iterable(values, name=name, context=ns, dtype=dtype)
265+
return from_native(series, series_only=True)
303266
elif implementation is Implementation.DASK: # pragma: no cover
304267
msg = "Dask support in Narwhals is lazy-only, so `new_series` is not supported"
305268
raise NotImplementedError(msg)
306269
else: # pragma: no cover
270+
native_namespace = implementation.to_native_namespace()
307271
try:
308272
# implementation is UNKNOWN, Narwhals extension using this feature should
309273
# implement `from_dict` function in the top-level namespace.

0 commit comments

Comments
 (0)