From 6ad4c4b9090eb7f42fd4ff535ab32ae69079c640 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 1 Nov 2024 20:02:01 +0100 Subject: [PATCH 1/6] add native property --- docs/api-reference/dataframe.md | 1 + docs/api-reference/lazyframe.md | 1 + docs/api-reference/series.md | 1 + narwhals/dataframe.py | 10 ++++++++++ narwhals/series.py | 5 +++++ tests/frame/to_native_test.py | 1 + tests/series_only/to_native_test.py | 4 ++-- utils/check_api_reference.py | 1 + 8 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/api-reference/dataframe.md b/docs/api-reference/dataframe.md index 447acbc159..7b146eeac8 100644 --- a/docs/api-reference/dataframe.md +++ b/docs/api-reference/dataframe.md @@ -24,6 +24,7 @@ - join - join_asof - lazy + - native - null_count - pipe - rename diff --git a/docs/api-reference/lazyframe.md b/docs/api-reference/lazyframe.md index a6776e08ce..c6d3e2368f 100644 --- a/docs/api-reference/lazyframe.md +++ b/docs/api-reference/lazyframe.md @@ -18,6 +18,7 @@ - join_asof - lazy - pipe + - native - rename - schema - select diff --git a/docs/api-reference/series.md b/docs/api-reference/series.md index e8572dda8d..13b69f97d2 100644 --- a/docs/api-reference/series.md +++ b/docs/api-reference/series.md @@ -39,6 +39,7 @@ - min - mode - name + - native - n_unique - null_count - pipe diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index 3ddaa28141..3fd2fdd285 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -328,6 +328,11 @@ def _series(self) -> type[Series]: def _lazyframe(self) -> type[LazyFrame[Any]]: return LazyFrame + @property + def native(self: Self) -> DataFrameT: + """Returns native frame underlying Narwhals DataFrame.""" + return self._compliant_frame._native_frame # type: ignore[no-any-return] + def __init__( self, df: Any, @@ -2765,6 +2770,11 @@ class LazyFrame(BaseFrame[FrameT]): def _dataframe(self) -> type[DataFrame[Any]]: return DataFrame + @property + def native(self: Self) -> FrameT: + """Returns native frame underlying Narwhals LazyFrame.""" + return self._compliant_frame._native_frame # type: ignore[no-any-return] + def __init__( self, df: Any, diff --git a/narwhals/series.py b/narwhals/series.py index 6f5223202e..3a0c013b0d 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -41,6 +41,11 @@ def _dataframe(self) -> type[DataFrame[Any]]: return DataFrame + @property + def native(self: Self) -> Any: + """Returns native series underlying Narwhals Series.""" + return self._compliant_series._native_series + def __init__( self: Self, series: Any, diff --git a/tests/frame/to_native_test.py b/tests/frame/to_native_test.py index fb90caf107..4648501966 100644 --- a/tests/frame/to_native_test.py +++ b/tests/frame/to_native_test.py @@ -14,3 +14,4 @@ def test_to_native(constructor: Constructor) -> None: df = nw.from_native(df_raw) assert isinstance(df.to_native(), df_raw.__class__) + assert isinstance(df.native, df_raw.__class__) diff --git a/tests/series_only/to_native_test.py b/tests/series_only/to_native_test.py index e6955b4c32..06fcd4563d 100644 --- a/tests/series_only/to_native_test.py +++ b/tests/series_only/to_native_test.py @@ -13,5 +13,5 @@ def test_to_native(constructor_eager: ConstructorEager) -> None: orig_series = constructor_eager({"a": data})["a"] # type: ignore[index] nw_series = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] - result = nw_series.to_native() - assert isinstance(result, orig_series.__class__) + assert isinstance(nw_series.to_native(), orig_series.__class__) + assert isinstance(nw_series.native, orig_series.__class__) diff --git a/utils/check_api_reference.py b/utils/check_api_reference.py index b7d8595aa0..d9ef276788 100644 --- a/utils/check_api_reference.py +++ b/utils/check_api_reference.py @@ -19,6 +19,7 @@ "is_sorted", "item", "name", + "native", "rename", "scatter", "shape", From 783865a23c531645b2d80ca692a4081ac91f9b7c Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 1 Nov 2024 20:29:42 +0100 Subject: [PATCH 2/6] docstring, deprecation --- docs/basics/dataframe_conversion.md | 2 +- narwhals/dataframe.py | 105 +++++++++++++++++++++-- narwhals/series.py | 48 ++++++++++- narwhals/stable/v1/__init__.py | 124 ++++++++++++++++++++++++++++ tests/frame/to_native_test.py | 12 +++ tests/series_only/to_native_test.py | 13 +++ 6 files changed, 291 insertions(+), 13 deletions(-) diff --git a/docs/basics/dataframe_conversion.md b/docs/basics/dataframe_conversion.md index 690f5d0936..d07d376153 100644 --- a/docs/basics/dataframe_conversion.md +++ b/docs/basics/dataframe_conversion.md @@ -49,7 +49,7 @@ Similarly, if your library uses Polars internally, you can convert any user-supp ```python exec="1" source="above" session="conversion" result="python" def df_to_polars(df: IntoDataFrame) -> pl.DataFrame: - return nw.from_arrow(nw.from_native(df), native_namespace=pl).to_native() + return nw.from_arrow(nw.from_native(df), native_namespace=pl).native print(df_to_polars(df_duckdb)) # You can only execute this line of code once. diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index 3fd2fdd285..e1a797ef1d 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -11,11 +11,11 @@ from typing import Sequence from typing import TypeVar from typing import overload +from warnings import warn from narwhals.dependencies import get_polars from narwhals.dependencies import is_numpy_array from narwhals.schema import Schema -from narwhals.translate import to_native from narwhals.utils import flatten from narwhals.utils import is_sequence_but_not_str from narwhals.utils import parse_version @@ -330,7 +330,50 @@ def _lazyframe(self) -> type[LazyFrame[Any]]: @property def native(self: Self) -> DataFrameT: - """Returns native frame underlying Narwhals DataFrame.""" + """ + Convert Narwhals DataFrame to native one. + + Returns: + Object of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import pyarrow as pa + >>> import narwhals as nw + >>> data = {"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]} + >>> df_pd = pd.DataFrame(data) + >>> df_pl = pl.DataFrame(data) + >>> df_pa = pa.table(data) + + Calling `to_native` on a Narwhals DataFrame returns the native object: + + >>> nw.from_native(df_pd).to_native() + foo bar ham + 0 1 6.0 a + 1 2 7.0 b + 2 3 8.0 c + >>> nw.from_native(df_pl).to_native() + shape: (3, 3) + ┌─────┬─────┬─────┐ + │ foo ┆ bar ┆ ham │ + │ --- ┆ --- ┆ --- │ + │ i64 ┆ f64 ┆ str │ + ╞═════╪═════╪═════╡ + │ 1 ┆ 6.0 ┆ a │ + │ 2 ┆ 7.0 ┆ b │ + │ 3 ┆ 8.0 ┆ c │ + └─────┴─────┴─────┘ + >>> nw.from_native(df_pa).to_native() + pyarrow.Table + foo: int64 + bar: double + ham: string + ---- + foo: [[1,2,3]] + bar: [[6,7,8]] + ham: [["a","b","c"]] + """ return self._compliant_frame._native_frame # type: ignore[no-any-return] def __init__( @@ -435,7 +478,7 @@ def lazy(self) -> LazyFrame[Any]: """ return self._lazyframe(self._compliant_frame.lazy(), level=self._level) - def to_native(self) -> DataFrameT: + def to_native(self: Self) -> DataFrameT: """ Convert Narwhals DataFrame to native one. @@ -480,8 +523,13 @@ def to_native(self) -> DataFrameT: bar: [[6,7,8]] ham: [["a","b","c"]] """ - - return self._compliant_frame._native_frame # type: ignore[no-any-return] + warn( + "Use `.native` property instead. `.to_native()` is " + "deprecated and it will be removed in future versions", + DeprecationWarning, + stacklevel=2, + ) + return self.native def to_pandas(self) -> pd.DataFrame: """ @@ -2772,7 +2820,41 @@ def _dataframe(self) -> type[DataFrame[Any]]: @property def native(self: Self) -> FrameT: - """Returns native frame underlying Narwhals LazyFrame.""" + """ + Convert Narwhals LazyFrame to native one. + + Returns: + Object of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import pyarrow as pa + >>> import narwhals as nw + >>> data = {"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]} + >>> df_pd = pd.DataFrame(data) + >>> df_pl = pl.LazyFrame(data) + >>> df_pa = pa.table(data) + + Calling `to_native` on a Narwhals DataFrame returns the native object: + + >>> nw.from_native(df_pd).lazy().to_native() + foo bar ham + 0 1 6.0 a + 1 2 7.0 b + 2 3 8.0 c + >>> nw.from_native(df_pl).to_native().collect() + shape: (3, 3) + ┌─────┬─────┬─────┐ + │ foo ┆ bar ┆ ham │ + │ --- ┆ --- ┆ --- │ + │ i64 ┆ f64 ┆ str │ + ╞═════╪═════╪═════╡ + │ 1 ┆ 6.0 ┆ a │ + │ 2 ┆ 7.0 ┆ b │ + │ 3 ┆ 8.0 ┆ c │ + └─────┴─────┴─────┘ + """ return self._compliant_frame._native_frame # type: ignore[no-any-return] def __init__( @@ -2847,7 +2929,7 @@ def collect(self) -> DataFrame[Any]: level=self._level, ) - def to_native(self) -> FrameT: + def to_native(self: Self) -> FrameT: """ Convert Narwhals LazyFrame to native one. @@ -2883,8 +2965,13 @@ def to_native(self) -> FrameT: │ 3 ┆ 8.0 ┆ c │ └─────┴─────┴─────┘ """ - - return to_native(narwhals_object=self, strict=True) + warn( + "Use `.native` property instead. `.to_native()` is " + "deprecated and it will be removed in future versions", + DeprecationWarning, + stacklevel=2, + ) + return self.native # inherited def pipe(self, function: Callable[[Any], Self], *args: Any, **kwargs: Any) -> Self: diff --git a/narwhals/series.py b/narwhals/series.py index 3a0c013b0d..86b2f6b3f4 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -9,6 +9,7 @@ from typing import Sequence from typing import TypeVar from typing import overload +from warnings import warn from narwhals.utils import parse_version @@ -43,7 +44,42 @@ def _dataframe(self) -> type[DataFrame[Any]]: @property def native(self: Self) -> Any: - """Returns native series underlying Narwhals Series.""" + """ + Convert Narwhals series to native series. + + Returns: + Series of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import narwhals as nw + >>> s = [1, 2, 3] + >>> s_pd = pd.Series(s) + >>> s_pl = pl.Series(s) + + We define a library agnostic function: + + >>> @nw.narwhalify + ... def func(s): + ... return s.to_native() + + We can then pass either pandas or Polars to `func`: + + >>> func(s_pd) + 0 1 + 1 2 + 2 3 + dtype: int64 + >>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE + shape: (3,) + Series: '' [i64] + [ + 1 + 2 + 3 + ] + """ return self._compliant_series._native_series def __init__( @@ -102,7 +138,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ca = pa.chunked_array([self.to_arrow()]) return ca.__arrow_c_stream__(requested_schema=requested_schema) - def to_native(self) -> Any: + def to_native(self: Self) -> Any: """ Convert Narwhals series to native series. @@ -139,7 +175,13 @@ def to_native(self) -> Any: 3 ] """ - return self._compliant_series._native_series + warn( + "Use `.native` property instead. `.to_native()` is " + "deprecated and it will be removed in future versions", + DeprecationWarning, + stacklevel=2, + ) + return self.native def scatter(self, indices: int | Sequence[int], values: Any) -> Self: """ diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py index 68aca7706e..a0789a2454 100644 --- a/narwhals/stable/v1/__init__.py +++ b/narwhals/stable/v1/__init__.py @@ -326,6 +326,53 @@ def is_unique(self: Self) -> Series: """ return super().is_unique() # type: ignore[return-value] + def to_native(self: Self) -> IntoDataFrameT: + """ + Convert Narwhals DataFrame to native one. + + Returns: + Object of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import pyarrow as pa + >>> import narwhals as nw + >>> data = {"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]} + >>> df_pd = pd.DataFrame(data) + >>> df_pl = pl.DataFrame(data) + >>> df_pa = pa.table(data) + + Calling `to_native` on a Narwhals DataFrame returns the native object: + + >>> nw.from_native(df_pd).to_native() + foo bar ham + 0 1 6.0 a + 1 2 7.0 b + 2 3 8.0 c + >>> nw.from_native(df_pl).to_native() + shape: (3, 3) + ┌─────┬─────┬─────┐ + │ foo ┆ bar ┆ ham │ + │ --- ┆ --- ┆ --- │ + │ i64 ┆ f64 ┆ str │ + ╞═════╪═════╪═════╡ + │ 1 ┆ 6.0 ┆ a │ + │ 2 ┆ 7.0 ┆ b │ + │ 3 ┆ 8.0 ┆ c │ + └─────┴─────┴─────┘ + >>> nw.from_native(df_pa).to_native() + pyarrow.Table + foo: int64 + bar: double + ham: string + ---- + foo: [[1,2,3]] + bar: [[6,7,8]] + ham: [["a","b","c"]] + """ + return self.native + def _l1_norm(self: Self) -> Self: """Private, just used to test the stable API.""" return self.select(all()._l1_norm()) @@ -383,6 +430,44 @@ def collect(self) -> DataFrame[Any]: """ return super().collect() # type: ignore[return-value] + def to_native(self: Self) -> IntoFrameT: + """ + Convert Narwhals LazyFrame to native one. + + Returns: + Object of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import pyarrow as pa + >>> import narwhals.stable.v1 as nw + >>> data = {"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]} + >>> df_pd = pd.DataFrame(data) + >>> df_pl = pl.LazyFrame(data) + >>> df_pa = pa.table(data) + + Calling `to_native` on a Narwhals DataFrame returns the native object: + + >>> nw.from_native(df_pd).lazy().to_native() + foo bar ham + 0 1 6.0 a + 1 2 7.0 b + 2 3 8.0 c + >>> nw.from_native(df_pl).to_native().collect() + shape: (3, 3) + ┌─────┬─────┬─────┐ + │ foo ┆ bar ┆ ham │ + │ --- ┆ --- ┆ --- │ + │ i64 ┆ f64 ┆ str │ + ╞═════╪═════╪═════╡ + │ 1 ┆ 6.0 ┆ a │ + │ 2 ┆ 7.0 ┆ b │ + │ 3 ┆ 8.0 ┆ c │ + └─────┴─────┴─────┘ + """ + return self.native + def _l1_norm(self: Self) -> Self: """Private, just used to test the stable API.""" return self.select(all()._l1_norm()) @@ -501,6 +586,45 @@ def value_counts( sort=sort, parallel=parallel, name=name, normalize=normalize ) + def to_native(self: Self) -> Any: + """ + Convert Narwhals series to native series. + + Returns: + Series of class that user started with. + + Examples: + >>> import pandas as pd + >>> import polars as pl + >>> import narwhals as nw + >>> s = [1, 2, 3] + >>> s_pd = pd.Series(s) + >>> s_pl = pl.Series(s) + + We define a library agnostic function: + + >>> @nw.narwhalify + ... def func(s): + ... return s.to_native() + + We can then pass either pandas or Polars to `func`: + + >>> func(s_pd) + 0 1 + 1 2 + 2 3 + dtype: int64 + >>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE + shape: (3,) + Series: '' [i64] + [ + 1 + 2 + 3 + ] + """ + return self.native + class Expr(NwExpr): def _l1_norm(self) -> Self: diff --git a/tests/frame/to_native_test.py b/tests/frame/to_native_test.py index 4648501966..4a7a2bae93 100644 --- a/tests/frame/to_native_test.py +++ b/tests/frame/to_native_test.py @@ -2,6 +2,9 @@ from typing import TYPE_CHECKING +import pytest + +import narwhals as nw_unstable import narwhals.stable.v1 as nw if TYPE_CHECKING: @@ -15,3 +18,12 @@ def test_to_native(constructor: Constructor) -> None: assert isinstance(df.to_native(), df_raw.__class__) assert isinstance(df.native, df_raw.__class__) + + +def test_raise_warning(constructor: Constructor) -> None: + data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.1, 8, 9]} + df_raw = constructor(data) + df = nw_unstable.from_native(df_raw) + + with pytest.deprecated_call(): + assert isinstance(df.to_native(), df_raw.__class__) diff --git a/tests/series_only/to_native_test.py b/tests/series_only/to_native_test.py index 06fcd4563d..9203d341fa 100644 --- a/tests/series_only/to_native_test.py +++ b/tests/series_only/to_native_test.py @@ -2,6 +2,9 @@ from typing import TYPE_CHECKING +import pytest + +import narwhals as nw_unstable import narwhals.stable.v1 as nw if TYPE_CHECKING: @@ -15,3 +18,13 @@ def test_to_native(constructor_eager: ConstructorEager) -> None: nw_series = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] assert isinstance(nw_series.to_native(), orig_series.__class__) assert isinstance(nw_series.native, orig_series.__class__) + + +def test_raise_warning(constructor_eager: ConstructorEager) -> None: + orig_series = constructor_eager({"a": data})["a"] # type: ignore[index] + nw_series = nw_unstable.from_native(constructor_eager({"a": data}), eager_only=True)[ + "a" + ] + + with pytest.deprecated_call(): + assert isinstance(nw_series.to_native(), orig_series.__class__) From 2592495190862ee601fe558842337d6cb3694ff5 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 1 Nov 2024 20:38:57 +0100 Subject: [PATCH 3/6] doctest --- narwhals/dataframe.py | 22 +++++++++++----------- narwhals/series.py | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index e1a797ef1d..27ea4dbfaf 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -346,14 +346,14 @@ def native(self: Self) -> DataFrameT: >>> df_pl = pl.DataFrame(data) >>> df_pa = pa.table(data) - Calling `to_native` on a Narwhals DataFrame returns the native object: + Calling `.native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).to_native() + >>> nw.from_native(df_pd).native foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native() + >>> nw.from_native(df_pl).native shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ @@ -364,7 +364,7 @@ def native(self: Self) -> DataFrameT: │ 2 ┆ 7.0 ┆ b │ │ 3 ┆ 8.0 ┆ c │ └─────┴─────┴─────┘ - >>> nw.from_native(df_pa).to_native() + >>> nw.from_native(df_pa).native pyarrow.Table foo: int64 bar: double @@ -497,12 +497,12 @@ def to_native(self: Self) -> DataFrameT: Calling `to_native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).to_native() + >>> nw.from_native(df_pd).to_native() # doctest:+SKIP foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native() + >>> nw.from_native(df_pl).to_native() # doctest:+SKIP shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ @@ -2836,14 +2836,14 @@ def native(self: Self) -> FrameT: >>> df_pl = pl.LazyFrame(data) >>> df_pa = pa.table(data) - Calling `to_native` on a Narwhals DataFrame returns the native object: + Calling `.native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).lazy().to_native() + >>> nw.from_native(df_pd).lazy().native foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native().collect() + >>> nw.from_native(df_pl).native.collect() shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ @@ -2948,12 +2948,12 @@ def to_native(self: Self) -> FrameT: Calling `to_native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).lazy().to_native() + >>> nw.from_native(df_pd).lazy().to_native() # doctest:+SKIP foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native().collect() + >>> nw.from_native(df_pl).to_native().collect() # doctest:+SKIP shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ diff --git a/narwhals/series.py b/narwhals/series.py index 86b2f6b3f4..f1045c02bf 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -62,7 +62,7 @@ def native(self: Self) -> Any: >>> @nw.narwhalify ... def func(s): - ... return s.to_native() + ... return s.native We can then pass either pandas or Polars to `func`: @@ -161,12 +161,12 @@ def to_native(self: Self) -> Any: We can then pass either pandas or Polars to `func`: - >>> func(s_pd) + >>> func(s_pd) # doctest:+SKIP 0 1 1 2 2 3 dtype: int64 - >>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE + >>> func(s_pl) # doctest:+SKIP shape: (3,) Series: '' [i64] [ From b099f87228bf94c2e900c16069f6e339473c063a Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 1 Nov 2024 20:43:03 +0100 Subject: [PATCH 4/6] doctest stable api --- narwhals/dataframe.py | 2 +- narwhals/stable/v1/__init__.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index 27ea4dbfaf..66dc4d5de2 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -513,7 +513,7 @@ def to_native(self: Self) -> DataFrameT: │ 2 ┆ 7.0 ┆ b │ │ 3 ┆ 8.0 ┆ c │ └─────┴─────┴─────┘ - >>> nw.from_native(df_pa).to_native() + >>> nw.from_native(df_pa).to_native() # doctest:+SKIP pyarrow.Table foo: int64 bar: double diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py index a0789a2454..b38826c62a 100644 --- a/narwhals/stable/v1/__init__.py +++ b/narwhals/stable/v1/__init__.py @@ -345,12 +345,12 @@ def to_native(self: Self) -> IntoDataFrameT: Calling `to_native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).to_native() + >>> nw.from_native(df_pd).to_native() # doctest:+SKIP foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native() + >>> nw.from_native(df_pl).to_native() # doctest:+SKIP shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ @@ -361,7 +361,7 @@ def to_native(self: Self) -> IntoDataFrameT: │ 2 ┆ 7.0 ┆ b │ │ 3 ┆ 8.0 ┆ c │ └─────┴─────┴─────┘ - >>> nw.from_native(df_pa).to_native() + >>> nw.from_native(df_pa).to_native() # doctest:+SKIP pyarrow.Table foo: int64 bar: double @@ -449,12 +449,12 @@ def to_native(self: Self) -> IntoFrameT: Calling `to_native` on a Narwhals DataFrame returns the native object: - >>> nw.from_native(df_pd).lazy().to_native() + >>> nw.from_native(df_pd).lazy().to_native() # doctest:+SKIP foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 c - >>> nw.from_native(df_pl).to_native().collect() + >>> nw.from_native(df_pl).to_native().collect() # doctest:+SKIP shape: (3, 3) ┌─────┬─────┬─────┐ │ foo ┆ bar ┆ ham │ @@ -609,12 +609,12 @@ def to_native(self: Self) -> Any: We can then pass either pandas or Polars to `func`: - >>> func(s_pd) + >>> func(s_pd) # doctest:+SKIP 0 1 1 2 2 3 dtype: int64 - >>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE + >>> func(s_pl) # doctest:+SKIP shape: (3,) Series: '' [i64] [ From 940a6e8aeaba22791d018d7ae5b071fcfe02cd73 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Sat, 2 Nov 2024 15:53:40 +0100 Subject: [PATCH 5/6] doctest:+SKIP for df.to_native() call --- narwhals/stable/v1/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py index b38826c62a..e178755079 100644 --- a/narwhals/stable/v1/__init__.py +++ b/narwhals/stable/v1/__init__.py @@ -416,7 +416,7 @@ def collect(self) -> DataFrame[Any]: | Use `.to_native` to see native output | └───────────────────────────────────────┘ >>> df = lf.group_by("a").agg(nw.all().sum()).collect() - >>> df.to_native().sort("a") + >>> df.to_native().sort("a") # doctest:+SKIP shape: (3, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ @@ -441,7 +441,7 @@ def to_native(self: Self) -> IntoFrameT: >>> import pandas as pd >>> import polars as pl >>> import pyarrow as pa - >>> import narwhals.stable.v1 as nw + >>> import narwhals as nw >>> data = {"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]} >>> df_pd = pd.DataFrame(data) >>> df_pl = pl.LazyFrame(data) From b06e674e88a94fddf412f7a716aa1808eb44245e Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Sat, 2 Nov 2024 15:58:21 +0100 Subject: [PATCH 6/6] from '.to_native()' to '.native' --- narwhals/dataframe.py | 2 +- narwhals/stable/v1/__init__.py | 2 +- tests/from_pycapsule_test.py | 6 +++--- tests/stable_api_test.py | 2 +- tests/utils_test.py | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py index 66dc4d5de2..6bada59f79 100644 --- a/narwhals/dataframe.py +++ b/narwhals/dataframe.py @@ -2912,7 +2912,7 @@ def collect(self) -> DataFrame[Any]: | Use `.to_native` to see native output | └───────────────────────────────────────┘ >>> df = lf.group_by("a").agg(nw.all().sum()).collect() - >>> df.to_native().sort("a") + >>> df.native.sort("a") shape: (3, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py index e178755079..e0f4dcb0a4 100644 --- a/narwhals/stable/v1/__init__.py +++ b/narwhals/stable/v1/__init__.py @@ -416,7 +416,7 @@ def collect(self) -> DataFrame[Any]: | Use `.to_native` to see native output | └───────────────────────────────────────┘ >>> df = lf.group_by("a").agg(nw.all().sum()).collect() - >>> df.to_native().sort("a") # doctest:+SKIP + >>> df.native.sort("a") shape: (3, 3) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ diff --git a/tests/from_pycapsule_test.py b/tests/from_pycapsule_test.py index 7d91a44f35..b48db2c362 100644 --- a/tests/from_pycapsule_test.py +++ b/tests/from_pycapsule_test.py @@ -16,7 +16,7 @@ def test_from_arrow_to_arrow() -> None: df = nw.from_native(pl.DataFrame({"ab": [1, 2, 3], "ba": [4, 5, 6]}), eager_only=True) result = nw.from_arrow(df, native_namespace=pa) - assert isinstance(result.to_native(), pa.Table) + assert isinstance(result.native, pa.Table) expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} assert_equal_data(result, expected) @@ -27,7 +27,7 @@ def test_from_arrow_to_polars(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delitem(sys.modules, "pandas") df = nw.from_native(tbl, eager_only=True) result = nw.from_arrow(df, native_namespace=pl) - assert isinstance(result.to_native(), pl.DataFrame) + assert isinstance(result.native, pl.DataFrame) expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} assert_equal_data(result, expected) assert "pandas" not in sys.modules @@ -37,7 +37,7 @@ def test_from_arrow_to_polars(monkeypatch: pytest.MonkeyPatch) -> None: def test_from_arrow_to_pandas() -> None: df = nw.from_native(pa.table({"ab": [1, 2, 3], "ba": [4, 5, 6]}), eager_only=True) result = nw.from_arrow(df, native_namespace=pd) - assert isinstance(result.to_native(), pd.DataFrame) + assert isinstance(result.native, pd.DataFrame) expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} assert_equal_data(result, expected) diff --git a/tests/stable_api_test.py b/tests/stable_api_test.py index 414b20ad02..108c0a5192 100644 --- a/tests/stable_api_test.py +++ b/tests/stable_api_test.py @@ -49,7 +49,7 @@ def test_renamed_taxicab_norm_dataframe(constructor: Constructor) -> None: def func(df_any: Any) -> Any: df = nw_v1.from_native(df_any) df = df._l1_norm() - return df.to_native() + return df.native result = nw_v1.from_native(func(constructor({"a": [1, 2, 3, -4, 5]}))) expected = {"a": [15]} diff --git a/tests/utils_test.py b/tests/utils_test.py index fb668b4d2b..dcfbe4acd2 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -102,7 +102,7 @@ def test_maybe_reset_index_pandas() -> None: result = nw.maybe_reset_index(pandas_df) expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) assert_frame_equal(nw.to_native(result), expected) - assert result.to_native() is pandas_df.to_native() + assert result.native is pandas_df.native pandas_series = nw.from_native( pd.Series([1, 2, 3], index=[7, 8, 9]), series_only=True ) @@ -113,7 +113,7 @@ def test_maybe_reset_index_pandas() -> None: result_s = nw.maybe_reset_index(pandas_series) expected_s = pd.Series([1, 2, 3]) assert_series_equal(nw.to_native(result_s), expected_s) - assert result_s.to_native() is pandas_series.to_native() + assert result_s.native is pandas_series.native def test_maybe_reset_index_polars() -> None: