@@ -593,15 +593,32 @@ def _from_numpy_impl(
593593 return from_native (native_frame , eager_only = True )
594594
595595
596+ @deprecate_native_namespace (warn_version = "1.31.0" , required = True )
596597def from_arrow (
597- native_frame : ArrowStreamExportable , * , native_namespace : ModuleType
598- ) -> DataFrame [Any ]:
598+ native_frame : ArrowStreamExportable ,
599+ * ,
600+ backend : ModuleType | Implementation | str | None = None ,
601+ native_namespace : ModuleType | None = None , # noqa: ARG001
602+ ) -> DataFrame [Any ]: # pragma: no cover
599603 """Construct a DataFrame from an object which supports the PyCapsule Interface.
600604
601605 Arguments:
602606 native_frame: Object which implements `__arrow_c_stream__`.
607+ backend: specifies which eager backend instantiate to.
608+
609+ `backend` can be specified in various ways:
610+
611+ - As `Implementation.<BACKEND>` with `BACKEND` being `PANDAS`, `PYARROW`,
612+ `POLARS`, `MODIN` or `CUDF`.
613+ - As a string: `"pandas"`, `"pyarrow"`, `"polars"`, `"modin"` or `"cudf"`.
614+ - Directly as a module `pandas`, `pyarrow`, `polars`, `modin` or `cudf`.
603615 native_namespace: The native library to use for DataFrame creation.
604616
617+ **Deprecated** (v1.31.0):
618+ Please use `backend` instead. Note that `native_namespace` is still available
619+ (and won't emit a deprecation warning) if you use `narwhals.stable.v1`,
620+ see [perfect backwards compatibility policy](../backcompat.md/).
621+
605622 Returns:
606623 A new DataFrame.
607624
@@ -611,7 +628,7 @@ def from_arrow(
611628 >>> import narwhals as nw
612629 >>>
613630 >>> df_native = pd.DataFrame({"a": [1, 2], "b": [4.2, 5.1]})
614- >>> nw.from_arrow(df_native, native_namespace=pl )
631+ >>> nw.from_arrow(df_native, backend="polars" )
615632 ┌──────────────────┐
616633 |Narwhals DataFrame|
617634 |------------------|
@@ -626,13 +643,22 @@ def from_arrow(
626643 | └─────┴─────┘ |
627644 └──────────────────┘
628645 """
629- if not hasattr (native_frame , "__arrow_c_stream__" ):
630- msg = f"Given object of type { type (native_frame )} does not support PyCapsule interface"
646+ backend = cast ("ModuleType | Implementation | str" , backend )
647+ return _from_arrow_impl (native_frame , backend = backend )
648+
649+
650+ def _from_arrow_impl (
651+ data : ArrowStreamExportable , * , backend : ModuleType | Implementation | str
652+ ) -> DataFrame [Any ]:
653+ if not hasattr (data , "__arrow_c_stream__" ):
654+ msg = f"Given object of type { type (data )} does not support PyCapsule interface"
631655 raise TypeError (msg )
632- implementation = Implementation .from_native_namespace (native_namespace )
656+
657+ implementation = Implementation .from_backend (backend )
658+ native_namespace = implementation .to_native_namespace ()
633659
634660 if implementation .is_polars () and parse_version (native_namespace ) >= (1 , 3 ):
635- native_frame = native_namespace .DataFrame (native_frame )
661+ native_frame = native_namespace .DataFrame (data )
636662 elif implementation in {
637663 Implementation .PANDAS ,
638664 Implementation .MODIN ,
@@ -650,7 +676,7 @@ def from_arrow(
650676 msg = f"PyArrow>=14.0.0 is required for `from_arrow` for object of type { native_namespace } "
651677 raise ModuleNotFoundError (msg ) from None
652678
653- tbl = pa .table (native_frame )
679+ tbl = pa .table (data )
654680 if implementation is Implementation .PANDAS :
655681 native_frame = tbl .to_pandas ()
656682 elif implementation is Implementation .MODIN : # pragma: no cover
@@ -665,12 +691,12 @@ def from_arrow(
665691 msg = "congratulations, you entered unrecheable code - please report a bug"
666692 raise AssertionError (msg )
667693 elif implementation is Implementation .PYARROW :
668- native_frame = native_namespace .table (native_frame )
694+ native_frame = native_namespace .table (data )
669695 else : # pragma: no cover
670696 try :
671697 # implementation is UNKNOWN, Narwhals extension using this feature should
672698 # implement PyCapsule support
673- native_frame = native_namespace .DataFrame (native_frame )
699+ native_frame = native_namespace .DataFrame (data )
674700 except AttributeError as e :
675701 msg = "Unknown namespace is expected to implement `DataFrame` class which accepts object which supports PyCapsule Interface."
676702 raise AttributeError (msg ) from e
0 commit comments