Skip to content

Commit

Permalink
enh: nw.dependencies.is_narwhals_dataframe / nw.dependencies.is_narwh…
Browse files Browse the repository at this point in the history
…als_lazyframe / nw.dependencies.is_narwhals_series (#1550)



---------

Co-authored-by: Marco Gorelli <[email protected]>
  • Loading branch information
marvinl803 and MarcoGorelli authored Dec 10, 2024
1 parent d788eec commit 68bc862
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
42 changes: 42 additions & 0 deletions narwhals/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import pyarrow as pa
import pyspark.sql as pyspark_sql

from narwhals.dataframe import DataFrame
from narwhals.dataframe import LazyFrame
from narwhals.series import Series
from narwhals.typing import IntoSeries

# We silently allow these but - given that they claim
Expand Down Expand Up @@ -320,6 +323,42 @@ def is_into_dataframe(native_dataframe: Any) -> bool:
)


def is_narwhals_dataframe(df: Any) -> TypeGuard[DataFrame[Any]]:
"""Check whether `df` is a Narwhals DataFrame.
This is useful if you expect a user to pass in a Narwhals
DataFrame directly, and you want to catch both ``narwhals.DataFrame``
and ``narwhals.stable.v1.DataFrame`.
"""
from narwhals.dataframe import DataFrame

return isinstance(df, DataFrame)


def is_narwhals_lazyframe(lf: Any) -> TypeGuard[LazyFrame[Any]]:
"""Check whether `lf` is a Narwhals LazyFrame.
This is useful if you expect a user to pass in a Narwhals
LazyFrame directly, and you want to catch both ``narwhals.LazyFrame``
and ``narwhals.stable.v1.LazyFrame`.
"""
from narwhals.dataframe import LazyFrame

return isinstance(lf, LazyFrame)


def is_narwhals_series(ser: Any) -> TypeGuard[Series[Any]]:
"""Check whether `ser` is a Narwhals Series.
This is useful if you expect a user to pass in a Narwhals
Series directly, and you want to catch both ``narwhals.Series``
and ``narwhals.stable.v1.Series`.
"""
from narwhals.series import Series

return isinstance(ser, Series)


__all__ = [
"get_cudf",
"get_ibis",
Expand All @@ -336,6 +375,9 @@ def is_into_dataframe(native_dataframe: Any) -> bool:
"is_into_series",
"is_modin_dataframe",
"is_modin_series",
"is_narwhals_dataframe",
"is_narwhals_lazyframe",
"is_narwhals_series",
"is_numpy_array",
"is_pandas_dataframe",
"is_pandas_index",
Expand Down
6 changes: 6 additions & 0 deletions narwhals/stable/v1/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from narwhals.dependencies import is_into_series
from narwhals.dependencies import is_modin_dataframe
from narwhals.dependencies import is_modin_series
from narwhals.dependencies import is_narwhals_dataframe
from narwhals.dependencies import is_narwhals_lazyframe
from narwhals.dependencies import is_narwhals_series
from narwhals.dependencies import is_numpy_array
from narwhals.dependencies import is_pandas_dataframe
from narwhals.dependencies import is_pandas_index
Expand Down Expand Up @@ -43,6 +46,9 @@
"is_into_series",
"is_modin_dataframe",
"is_modin_series",
"is_narwhals_dataframe",
"is_narwhals_lazyframe",
"is_narwhals_series",
"is_numpy_array",
"is_pandas_dataframe",
"is_pandas_index",
Expand Down
18 changes: 18 additions & 0 deletions tests/dependencies/is_narwhals_dataframe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import narwhals as nw
import narwhals.stable.v1 as nws
from narwhals.stable.v1.dependencies import is_narwhals_dataframe

if TYPE_CHECKING:
from tests.utils import ConstructorEager


def test_is_narwhals_dataframe(constructor_eager: ConstructorEager) -> None:
df = constructor_eager({"col1": [1, 2], "col2": [3, 4]})

assert is_narwhals_dataframe(nw.from_native(df))
assert is_narwhals_dataframe(nws.from_native(df))
assert not is_narwhals_dataframe(df)
19 changes: 19 additions & 0 deletions tests/dependencies/is_narwhals_lazyframe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import narwhals as nw
import narwhals.stable.v1 as nws
from narwhals.stable.v1.dependencies import is_narwhals_lazyframe
from tests.utils import Constructor

if TYPE_CHECKING:
from tests.utils import Constructor


def test_is_narwhals_lazyframe(constructor: Constructor) -> None:
lf = constructor({"a": [1, 2, 3]})

assert is_narwhals_lazyframe(nw.from_native(lf).lazy())
assert is_narwhals_lazyframe(nws.from_native(lf).lazy())
assert not is_narwhals_lazyframe(lf)
18 changes: 18 additions & 0 deletions tests/dependencies/is_narwhals_series_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import narwhals as nw
import narwhals.stable.v1 as nws
from narwhals.stable.v1.dependencies import is_narwhals_series

if TYPE_CHECKING:
from tests.utils import ConstructorEager


def test_is_narwhals_series(constructor_eager: ConstructorEager) -> None:
df = constructor_eager({"col1": [1, 2], "col2": [3, 4]})

assert is_narwhals_series(nw.from_native(df, eager_only=True)["col1"])
assert is_narwhals_series(nws.from_native(df, eager_only=True)["col1"])
assert not is_narwhals_series(nw.from_native(df, eager_only=True)["col1"].to_native())

0 comments on commit 68bc862

Please sign in to comment.