Skip to content

Commit

Permalink
add native property
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Nov 1, 2024
1 parent 5c3db5b commit 6ad4c4b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 2 deletions.
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]

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

0 comments on commit 6ad4c4b

Please sign in to comment.