|
44 | 44 | if TYPE_CHECKING: |
45 | 45 | from types import ModuleType |
46 | 46 |
|
47 | | - import pyarrow as pa |
48 | 47 | from typing_extensions import Self |
49 | 48 | from typing_extensions import TypeAlias |
50 | 49 | from typing_extensions import TypeIs |
@@ -260,58 +259,21 @@ def _new_series_impl( |
260 | 259 | version: Version, |
261 | 260 | ) -> Series[Any]: |
262 | 261 | 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) |
303 | 266 | elif implementation is Implementation.DASK: # pragma: no cover |
304 | 267 | msg = "Dask support in Narwhals is lazy-only, so `new_series` is not supported" |
305 | 268 | raise NotImplementedError(msg) |
306 | 269 | else: # pragma: no cover |
| 270 | + native_namespace = implementation.to_native_namespace() |
307 | 271 | try: |
308 | | - # implementation is UNKNOWN, Narwhals extension using this feature should |
309 | | - # implement `from_dict` function in the top-level namespace. |
310 | 272 | native_series = native_namespace.new_series(name, values, dtype) |
| 273 | + return from_native(native_series, series_only=True).alias(name) |
311 | 274 | except AttributeError as e: |
312 | | - msg = "Unknown namespace is expected to implement `Series` constructor." |
| 275 | + msg = "Unknown namespace is expected to implement `new_series` constructor." |
313 | 276 | raise AttributeError(msg) from e |
314 | | - return from_native(native_series, series_only=True).alias(name) |
315 | 277 |
|
316 | 278 |
|
317 | 279 | @deprecate_native_namespace(warn_version="1.26.0") |
|
0 commit comments