Skip to content

Commit 5c3db5b

Browse files
authored
feat: add is_into_dataframe (#1288)
* add is_into_dataframe * fix pyarrow table construct * typo series-> dataframe
1 parent 6b5e2bf commit 5c3db5b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

docs/api-reference/dependencies.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- is_cudf_series
1616
- is_dask_dataframe
1717
- is_ibis_table
18+
- is_into_dataframe
1819
- is_into_series
1920
- is_modin_dataframe
2021
- is_modin_index

narwhals/dependencies.py

+39
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,44 @@ def is_into_series(native_series: IntoSeries) -> bool:
251251
)
252252

253253

254+
def is_into_dataframe(native_dataframe: Any) -> bool:
255+
"""
256+
Check whether `native_dataframe` can be converted to a Narwhals DataFrame.
257+
258+
Arguments:
259+
native_dataframe: The object to check.
260+
261+
Returns:
262+
`True` if `native_dataframe` can be converted to a Narwhals DataFrame, `False` otherwise.
263+
264+
Examples:
265+
>>> import pandas as pd
266+
>>> import polars as pl
267+
>>> import numpy as np
268+
>>> from narwhals.dependencies import is_into_dataframe
269+
270+
>>> df_pd = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
271+
>>> df_pl = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
272+
>>> np_arr = np.array([[1, 4], [2, 5], [3, 6]])
273+
274+
>>> is_into_dataframe(df_pd)
275+
True
276+
>>> is_into_dataframe(df_pl)
277+
True
278+
>>> is_into_dataframe(np_arr)
279+
False
280+
"""
281+
from narwhals.dataframe import DataFrame
282+
283+
return (
284+
isinstance(native_dataframe, DataFrame)
285+
or hasattr(native_dataframe, "__narwhals_dataframe__")
286+
or is_polars_dataframe(native_dataframe)
287+
or is_pyarrow_table(native_dataframe)
288+
or is_pandas_like_dataframe(native_dataframe)
289+
)
290+
291+
254292
__all__ = [
255293
"get_polars",
256294
"get_pandas",
@@ -275,5 +313,6 @@ def is_into_series(native_series: IntoSeries) -> bool:
275313
"is_dask_dataframe",
276314
"is_pandas_like_dataframe",
277315
"is_pandas_like_series",
316+
"is_into_dataframe",
278317
"is_into_series",
279318
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
from typing import Any
5+
6+
import numpy as np
7+
import pandas as pd
8+
import polars as pl
9+
import pyarrow as pa
10+
11+
import narwhals as nw
12+
from narwhals.dependencies import is_into_dataframe
13+
14+
if TYPE_CHECKING:
15+
from typing_extensions import Self
16+
17+
18+
class DictDataFrame:
19+
def __init__(self, data: dict[str, list[Any]]) -> None:
20+
self._data = data
21+
22+
def __len__(self) -> int: # pragma: no cover
23+
return len(next(iter(self._data.values())))
24+
25+
def __narwhals_dataframe__(self) -> Self: # pragma: no cover
26+
return self
27+
28+
29+
def test_is_into_dataframe() -> None:
30+
data = {"a": [1, 2, 3], "b": [4, 5, 6]}
31+
assert is_into_dataframe(pa.table(data))
32+
assert is_into_dataframe(pl.DataFrame(data))
33+
assert is_into_dataframe(pd.DataFrame(data))
34+
assert is_into_dataframe(nw.from_native(pd.DataFrame(data)))
35+
assert is_into_dataframe(DictDataFrame(data))
36+
assert not is_into_dataframe(np.array([[1, 4], [2, 5], [3, 6]]))
37+
assert not is_into_dataframe(data)

0 commit comments

Comments
 (0)