Skip to content

Commit 6f88d02

Browse files
committed
wip
1 parent fb97947 commit 6f88d02

File tree

7 files changed

+51
-50
lines changed

7 files changed

+51
-50
lines changed

narwhals/pandas_like/__init__.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +0,0 @@
1-
from __future__ import annotations
2-
3-
from typing import TYPE_CHECKING
4-
from typing import Any
5-
6-
from narwhals.pandas_like.dataframe import LazyFrame
7-
from narwhals.pandas_like.namespace import Namespace # noqa: F401
8-
9-
if TYPE_CHECKING:
10-
from narwhals.spec import LazyFrame as LazyFrameT
11-
from narwhals.spec import Namespace as NamespaceT
12-
13-
14-
def translate(
15-
df: Any,
16-
implementation: str,
17-
api_version: str,
18-
) -> tuple[LazyFrameT, NamespaceT]:
19-
df = LazyFrame(
20-
df,
21-
api_version=api_version,
22-
implementation=implementation,
23-
)
24-
return df, df.__lazyframe_namespace__()

narwhals/pandas_like/dataframe.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
from typing import Iterable
77
from typing import Literal
88

9-
import narwhals
109
from narwhals.spec import DataFrame as DataFrameT
1110
from narwhals.spec import GroupBy as GroupByT
1211
from narwhals.spec import IntoExpr
13-
from narwhals.spec import LazyFrame as LazyFrameT
12+
from narwhals.spec import LazyFrame as LazyFrameProtocol
1413
from narwhals.spec import LazyGroupBy as LazyGroupByT
15-
from narwhals.spec import Namespace as NamespaceT
14+
from narwhals.spec import Namespace as NamespaceProtocol
1615
from narwhals.utils import evaluate_into_exprs
1716
from narwhals.utils import flatten_str
1817
from narwhals.utils import horizontal_concat
@@ -23,6 +22,8 @@
2322

2423
from typing_extensions import Self
2524

25+
from narwhals.pandas_like.namespace import Namespace
26+
2627

2728
class DataFrame(DataFrameT):
2829
"""dataframe object"""
@@ -84,8 +85,10 @@ def dataframe(self) -> Any:
8485

8586
def __dataframe_namespace__(
8687
self,
87-
) -> NamespaceT:
88-
return narwhals.pandas_like.namespace.Namespace(
88+
) -> Namespace:
89+
from narwhals.pandas_like.namespace import Namespace
90+
91+
return Namespace(
8992
api_version=self.api_version,
9093
implementation=self._implementation, # type: ignore[attr-defined]
9194
)
@@ -169,7 +172,7 @@ def to_pandas(self) -> Any:
169172
raise TypeError(msg)
170173

171174

172-
class LazyFrame(LazyFrameT):
175+
class LazyFrame(LazyFrameProtocol):
173176
"""dataframe object"""
174177

175178
def __init__(
@@ -233,8 +236,10 @@ def dataframe(self) -> Any:
233236

234237
def __lazyframe_namespace__(
235238
self,
236-
) -> NamespaceT:
237-
return narwhals.pandas_like.namespace.Namespace(
239+
) -> NamespaceProtocol:
240+
from narwhals.pandas_like.namespace import Namespace
241+
242+
return Namespace(
238243
api_version=self.api_version,
239244
implementation=self._implementation, # type: ignore[attr-defined]
240245
)

narwhals/pandas_like/expr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
from narwhals.spec import DataFrame as DataFrameT
88
from narwhals.spec import Expr as ExprT
99
from narwhals.spec import ExprStringNamespace as ExprStringNamespaceT
10-
from narwhals.spec import LazyFrame as LazyFrameT
11-
from narwhals.spec import Namespace as NamespaceT
10+
from narwhals.spec import LazyFrame as LazyFrameProtocol
11+
from narwhals.spec import Namespace as NamespaceProtocol
1212
from narwhals.spec import Series as SeriesT
1313
from narwhals.utils import register_expression_call
1414

1515

1616
class Expr(ExprT):
1717
def __init__( # noqa: PLR0913
1818
self,
19-
call: Callable[[DataFrameT | LazyFrameT], list[SeriesT]],
19+
call: Callable[[DataFrameT | LazyFrameProtocol], list[SeriesT]],
2020
*,
2121
depth: int | None,
2222
function_name: str | None,
@@ -62,7 +62,7 @@ def from_column_names(
6262
implementation=implementation,
6363
)
6464

65-
def __expr_namespace__(self) -> NamespaceT:
65+
def __expr_namespace__(self) -> NamespaceProtocol:
6666
from narwhals.pandas_like.namespace import Namespace
6767

6868
return Namespace(

narwhals/pandas_like/group_by_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from narwhals.spec import DataFrame as DataFrameT
99
from narwhals.spec import GroupBy as GroupByT
1010
from narwhals.spec import IntoExpr
11-
from narwhals.spec import LazyFrame as LazyFrameT
11+
from narwhals.spec import LazyFrame as LazyFrameProtocol
1212
from narwhals.spec import LazyGroupBy as LazyGroupByT
1313
from narwhals.utils import dataframe_from_dict
1414
from narwhals.utils import evaluate_simple_aggregation
@@ -37,7 +37,7 @@ def agg(
3737

3838

3939
class LazyGroupBy(LazyGroupByT):
40-
def __init__(self, df: LazyFrameT, keys: list[str], api_version: str) -> None:
40+
def __init__(self, df: LazyFrameProtocol, keys: list[str], api_version: str) -> None:
4141
self._df = df
4242
self._keys = list(keys)
4343
self.api_version = api_version
@@ -46,7 +46,7 @@ def agg(
4646
self,
4747
*aggs: IntoExpr | Iterable[IntoExpr],
4848
**named_aggs: IntoExpr,
49-
) -> LazyFrameT:
49+
) -> LazyFrameProtocol:
5050
df = self._df.dataframe # type: ignore[attr-defined]
5151
exprs = parse_into_exprs(
5252
get_namespace(self._df),

narwhals/pandas_like/namespace.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
from narwhals.spec import DataFrame as DataFrameT
1414
from narwhals.spec import Expr as ExprT
1515
from narwhals.spec import IntoExpr
16-
from narwhals.spec import LazyFrame as LazyFrameT
17-
from narwhals.spec import Namespace as NamespaceT
16+
from narwhals.spec import LazyFrame as LazyFrameProtocol
17+
from narwhals.spec import Namespace as NamespaceProtocol
1818
from narwhals.spec import Series as SeriesT
1919
from narwhals.utils import flatten_str
2020
from narwhals.utils import horizontal_concat
2121
from narwhals.utils import parse_into_exprs
2222
from narwhals.utils import series_from_iterable
2323

2424

25-
class Namespace(NamespaceT):
25+
class Namespace(NamespaceProtocol):
2626
def __init__(self, *, api_version: str, implementation: str) -> None:
2727
self.__dataframeapi_version__ = api_version
2828
self.api_version = api_version
@@ -110,7 +110,7 @@ def len(self) -> ExprT:
110110

111111
def _create_expr_from_callable( # noqa: PLR0913
112112
self,
113-
func: Callable[[DataFrameT | LazyFrameT], list[SeriesT]],
113+
func: Callable[[DataFrameT | LazyFrameProtocol], list[SeriesT]],
114114
*,
115115
depth: int,
116116
function_name: str | None,

narwhals/pandas_like/series.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
34
from typing import Any
45

56
from pandas.api.types import is_extension_array_dtype
67

7-
import narwhals
88
from narwhals.spec import Series as SeriesT
99
from narwhals.utils import item
1010
from narwhals.utils import validate_column_comparand
1111

12+
if TYPE_CHECKING:
13+
from narwhals.pandas_like.namespace import Namespace
14+
1215

1316
class Series(SeriesT):
1417
def __init__(
@@ -53,8 +56,10 @@ def _from_series(self, series: Any) -> Series:
5356

5457
def __series_namespace__(
5558
self,
56-
) -> narwhals.pandas_like.Namespace:
57-
return narwhals.pandas_like.Namespace(
59+
) -> Namespace:
60+
from narwhals.pandas_like.namespace import Namespace
61+
62+
return Namespace(
5863
api_version=self.api_version,
5964
implementation=self._implementation,
6065
)

narwhals/translate.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def to_polars_api(df: Any, version: str) -> tuple[LazyFrame, Namespace]:
2727
pass
2828
else:
2929
if isinstance(df, pd.DataFrame):
30-
from narwhals.pandas_like import translate
30+
from narwhals.pandas_like.translate import translate
3131

3232
return translate(df, api_version=version, implementation="pandas")
3333
try:
@@ -36,7 +36,7 @@ def to_polars_api(df: Any, version: str) -> tuple[LazyFrame, Namespace]:
3636
pass
3737
else:
3838
if isinstance(df, cudf.DataFrame):
39-
from narwhals.pandas_like import translate
39+
from narwhals.pandas_like.translate import translate
4040

4141
return translate(df, api_version=version, implementation="cudf")
4242
try:
@@ -45,7 +45,7 @@ def to_polars_api(df: Any, version: str) -> tuple[LazyFrame, Namespace]:
4545
pass
4646
else:
4747
if isinstance(df, mpd.DataFrame):
48-
from narwhals.pandas_like import translate
48+
from narwhals.pandas_like.translate import translate
4949

5050
return translate(df, api_version=version, implementation="modin")
5151
msg = f"Could not translate DataFrame {type(df)}, please open a feature request."
@@ -55,7 +55,7 @@ def to_polars_api(df: Any, version: str) -> tuple[LazyFrame, Namespace]:
5555
def quick_translate(df: Any, version: str, implementation: str) -> DataFrame:
5656
"""Translate to Polars API, if implementation is already known."""
5757
if implementation in ("pandas", "cudf"):
58-
from narwhals.pandas_like import translate
58+
from narwhals.pandas_like.translate import translate
5959

6060
df, _pl = translate(df, api_version=version, implementation=implementation)
6161
return df
@@ -96,3 +96,18 @@ def get_namespace(obj: Any, implementation: str | None = None) -> Namespace:
9696
return obj.__expr_namespace__()
9797
msg = f"Could not find namespace for object {obj}"
9898
raise TypeError(msg)
99+
100+
101+
def translate(
102+
df: Any,
103+
implementation: str,
104+
api_version: str,
105+
) -> tuple[LazyFrame, Namespace]:
106+
from narwhals.pandas_like.dataframe import LazyFrame
107+
108+
df = LazyFrame(
109+
df,
110+
api_version=api_version,
111+
implementation=implementation,
112+
)
113+
return df, df.__lazyframe_namespace__()

0 commit comments

Comments
 (0)