Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add native property and deprecate .to_native() method #1301

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api-reference/dataframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- join
- join_asof
- lazy
- native
- null_count
- pipe
- rename
Expand Down
1 change: 1 addition & 0 deletions docs/api-reference/lazyframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- join_asof
- lazy
- pipe
- native
- rename
- schema
- select
Expand Down
1 change: 1 addition & 0 deletions docs/api-reference/series.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- min
- mode
- name
- native
- n_unique
- null_count
- pipe
Expand Down
10 changes: 10 additions & 0 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
FBruzzesi marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
df: Any,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/frame/to_native_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
4 changes: 2 additions & 2 deletions tests/series_only/to_native_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
1 change: 1 addition & 0 deletions utils/check_api_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"is_sorted",
"item",
"name",
"native",
"rename",
"scatter",
"shape",
Expand Down
Loading