Skip to content

Commit 9d7fc16

Browse files
committed
refactor: migrate expr namespaces
1 parent 4bc483f commit 9d7fc16

File tree

4 files changed

+65
-121
lines changed

4 files changed

+65
-121
lines changed

narwhals/_pandas_like/expr_cat.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from narwhals._expression_parsing import reuse_series_namespace_implementation
6-
75
if TYPE_CHECKING:
86
from typing_extensions import Self
97

@@ -15,8 +13,6 @@ def __init__(self: Self, expr: PandasLikeExpr) -> None:
1513
self._compliant_expr = expr
1614

1715
def get_categories(self: Self) -> PandasLikeExpr:
18-
return reuse_series_namespace_implementation(
19-
self._compliant_expr,
20-
"cat",
21-
"get_categories",
16+
return self._compliant_expr._reuse_series_namespace_implementation(
17+
"cat", "get_categories"
2218
)

narwhals/_pandas_like/expr_dt.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from narwhals._expression_parsing import reuse_series_namespace_implementation
6-
75
if TYPE_CHECKING:
86
from typing_extensions import Self
97

@@ -16,92 +14,92 @@ def __init__(self: Self, expr: PandasLikeExpr) -> None:
1614
self._compliant_expr = expr
1715

1816
def date(self: Self) -> PandasLikeExpr:
19-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "date")
17+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "date")
2018

2119
def year(self: Self) -> PandasLikeExpr:
22-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "year")
20+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "year")
2321

2422
def month(self: Self) -> PandasLikeExpr:
25-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "month")
23+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "month")
2624

2725
def day(self: Self) -> PandasLikeExpr:
28-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "day")
26+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "day")
2927

3028
def hour(self: Self) -> PandasLikeExpr:
31-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "hour")
29+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "hour")
3230

3331
def minute(self: Self) -> PandasLikeExpr:
34-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "minute")
32+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "minute")
3533

3634
def second(self: Self) -> PandasLikeExpr:
37-
return reuse_series_namespace_implementation(self._compliant_expr, "dt", "second")
35+
return self._compliant_expr._reuse_series_namespace_implementation("dt", "second")
3836

3937
def millisecond(self: Self) -> PandasLikeExpr:
40-
return reuse_series_namespace_implementation(
41-
self._compliant_expr, "dt", "millisecond"
38+
return self._compliant_expr._reuse_series_namespace_implementation(
39+
"dt", "millisecond"
4240
)
4341

4442
def microsecond(self: Self) -> PandasLikeExpr:
45-
return reuse_series_namespace_implementation(
46-
self._compliant_expr, "dt", "microsecond"
43+
return self._compliant_expr._reuse_series_namespace_implementation(
44+
"dt", "microsecond"
4745
)
4846

4947
def nanosecond(self: Self) -> PandasLikeExpr:
50-
return reuse_series_namespace_implementation(
51-
self._compliant_expr, "dt", "nanosecond"
48+
return self._compliant_expr._reuse_series_namespace_implementation(
49+
"dt", "nanosecond"
5250
)
5351

5452
def ordinal_day(self: Self) -> PandasLikeExpr:
55-
return reuse_series_namespace_implementation(
56-
self._compliant_expr, "dt", "ordinal_day"
53+
return self._compliant_expr._reuse_series_namespace_implementation(
54+
"dt", "ordinal_day"
5755
)
5856

5957
def weekday(self: Self) -> PandasLikeExpr:
60-
return reuse_series_namespace_implementation(
61-
self._compliant_expr, "dt", "weekday"
58+
return self._compliant_expr._reuse_series_namespace_implementation(
59+
"dt", "weekday"
6260
)
6361

6462
def total_minutes(self: Self) -> PandasLikeExpr:
65-
return reuse_series_namespace_implementation(
66-
self._compliant_expr, "dt", "total_minutes"
63+
return self._compliant_expr._reuse_series_namespace_implementation(
64+
"dt", "total_minutes"
6765
)
6866

6967
def total_seconds(self: Self) -> PandasLikeExpr:
70-
return reuse_series_namespace_implementation(
71-
self._compliant_expr, "dt", "total_seconds"
68+
return self._compliant_expr._reuse_series_namespace_implementation(
69+
"dt", "total_seconds"
7270
)
7371

7472
def total_milliseconds(self: Self) -> PandasLikeExpr:
75-
return reuse_series_namespace_implementation(
76-
self._compliant_expr, "dt", "total_milliseconds"
73+
return self._compliant_expr._reuse_series_namespace_implementation(
74+
"dt", "total_milliseconds"
7775
)
7876

7977
def total_microseconds(self: Self) -> PandasLikeExpr:
80-
return reuse_series_namespace_implementation(
81-
self._compliant_expr, "dt", "total_microseconds"
78+
return self._compliant_expr._reuse_series_namespace_implementation(
79+
"dt", "total_microseconds"
8280
)
8381

8482
def total_nanoseconds(self: Self) -> PandasLikeExpr:
85-
return reuse_series_namespace_implementation(
86-
self._compliant_expr, "dt", "total_nanoseconds"
83+
return self._compliant_expr._reuse_series_namespace_implementation(
84+
"dt", "total_nanoseconds"
8785
)
8886

8987
def to_string(self: Self, format: str) -> PandasLikeExpr: # noqa: A002
90-
return reuse_series_namespace_implementation(
91-
self._compliant_expr, "dt", "to_string", format=format
88+
return self._compliant_expr._reuse_series_namespace_implementation(
89+
"dt", "to_string", format=format
9290
)
9391

9492
def replace_time_zone(self: Self, time_zone: str | None) -> PandasLikeExpr:
95-
return reuse_series_namespace_implementation(
96-
self._compliant_expr, "dt", "replace_time_zone", time_zone=time_zone
93+
return self._compliant_expr._reuse_series_namespace_implementation(
94+
"dt", "replace_time_zone", time_zone=time_zone
9795
)
9896

9997
def convert_time_zone(self: Self, time_zone: str) -> PandasLikeExpr:
100-
return reuse_series_namespace_implementation(
101-
self._compliant_expr, "dt", "convert_time_zone", time_zone=time_zone
98+
return self._compliant_expr._reuse_series_namespace_implementation(
99+
"dt", "convert_time_zone", time_zone=time_zone
102100
)
103101

104102
def timestamp(self: Self, time_unit: TimeUnit) -> PandasLikeExpr:
105-
return reuse_series_namespace_implementation(
106-
self._compliant_expr, "dt", "timestamp", time_unit=time_unit
103+
return self._compliant_expr._reuse_series_namespace_implementation(
104+
"dt", "timestamp", time_unit=time_unit
107105
)

narwhals/_pandas_like/expr_list.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from narwhals._expression_parsing import reuse_series_namespace_implementation
6-
75
if TYPE_CHECKING:
86
from typing_extensions import Self
97

@@ -15,8 +13,4 @@ def __init__(self: Self, expr: PandasLikeExpr) -> None:
1513
self._expr = expr
1614

1715
def len(self: Self) -> PandasLikeExpr:
18-
return reuse_series_namespace_implementation(
19-
self._expr,
20-
"list",
21-
"len",
22-
)
16+
return self._expr._reuse_series_namespace_implementation("list", "len")

narwhals/_pandas_like/expr_str.py

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from narwhals._expression_parsing import reuse_series_namespace_implementation
6-
75
if TYPE_CHECKING:
86
from typing_extensions import Self
97

@@ -14,108 +12,66 @@ class PandasLikeExprStringNamespace:
1412
def __init__(self: Self, expr: PandasLikeExpr) -> None:
1513
self._compliant_expr = expr
1614

17-
def len_chars(
18-
self: Self,
19-
) -> PandasLikeExpr:
20-
return reuse_series_namespace_implementation(
21-
self._compliant_expr, "str", "len_chars"
15+
def len_chars(self: Self) -> PandasLikeExpr:
16+
return self._compliant_expr._reuse_series_namespace_implementation(
17+
"str", "len_chars"
2218
)
2319

2420
def replace(
25-
self: Self,
26-
pattern: str,
27-
value: str,
28-
*,
29-
literal: bool,
30-
n: int,
21+
self: Self, pattern: str, value: str, *, literal: bool, n: int
3122
) -> PandasLikeExpr:
32-
return reuse_series_namespace_implementation(
33-
self._compliant_expr,
34-
"str",
35-
"replace",
36-
pattern=pattern,
37-
value=value,
38-
literal=literal,
39-
n=n,
23+
return self._compliant_expr._reuse_series_namespace_implementation(
24+
"str", "replace", pattern=pattern, value=value, literal=literal, n=n
4025
)
4126

4227
def replace_all(
43-
self: Self,
44-
pattern: str,
45-
value: str,
46-
*,
47-
literal: bool,
28+
self: Self, pattern: str, value: str, *, literal: bool
4829
) -> PandasLikeExpr:
49-
return reuse_series_namespace_implementation(
50-
self._compliant_expr,
51-
"str",
52-
"replace_all",
53-
pattern=pattern,
54-
value=value,
55-
literal=literal,
30+
return self._compliant_expr._reuse_series_namespace_implementation(
31+
"str", "replace_all", pattern=pattern, value=value, literal=literal
5632
)
5733

5834
def strip_chars(self: Self, characters: str | None) -> PandasLikeExpr:
59-
return reuse_series_namespace_implementation(
60-
self._compliant_expr,
61-
"str",
62-
"strip_chars",
63-
characters=characters,
35+
return self._compliant_expr._reuse_series_namespace_implementation(
36+
"str", "strip_chars", characters=characters
6437
)
6538

6639
def starts_with(self: Self, prefix: str) -> PandasLikeExpr:
67-
return reuse_series_namespace_implementation(
68-
self._compliant_expr,
69-
"str",
70-
"starts_with",
71-
prefix=prefix,
40+
return self._compliant_expr._reuse_series_namespace_implementation(
41+
"str", "starts_with", prefix=prefix
7242
)
7343

7444
def ends_with(self: Self, suffix: str) -> PandasLikeExpr:
75-
return reuse_series_namespace_implementation(
76-
self._compliant_expr,
77-
"str",
78-
"ends_with",
79-
suffix=suffix,
45+
return self._compliant_expr._reuse_series_namespace_implementation(
46+
"str", "ends_with", suffix=suffix
8047
)
8148

8249
def contains(self: Self, pattern: str, *, literal: bool) -> PandasLikeExpr:
83-
return reuse_series_namespace_implementation(
84-
self._compliant_expr,
85-
"str",
86-
"contains",
87-
pattern=pattern,
88-
literal=literal,
50+
return self._compliant_expr._reuse_series_namespace_implementation(
51+
"str", "contains", pattern=pattern, literal=literal
8952
)
9053

9154
def slice(self: Self, offset: int, length: int | None) -> PandasLikeExpr:
92-
return reuse_series_namespace_implementation(
93-
self._compliant_expr, "str", "slice", offset=offset, length=length
55+
return self._compliant_expr._reuse_series_namespace_implementation(
56+
"str", "slice", offset=offset, length=length
9457
)
9558

9659
def split(self: Self, by: str) -> PandasLikeExpr:
97-
return reuse_series_namespace_implementation(
98-
self._compliant_expr, "str", "split", by=by
60+
return self._compliant_expr._reuse_series_namespace_implementation(
61+
"str", "split", by=by
9962
)
10063

10164
def to_datetime(self: Self, format: str | None) -> PandasLikeExpr: # noqa: A002
102-
return reuse_series_namespace_implementation(
103-
self._compliant_expr,
104-
"str",
105-
"to_datetime",
106-
format=format,
65+
return self._compliant_expr._reuse_series_namespace_implementation(
66+
"str", "to_datetime", format=format
10767
)
10868

10969
def to_uppercase(self: Self) -> PandasLikeExpr:
110-
return reuse_series_namespace_implementation(
111-
self._compliant_expr,
112-
"str",
113-
"to_uppercase",
70+
return self._compliant_expr._reuse_series_namespace_implementation(
71+
"str", "to_uppercase"
11472
)
11573

11674
def to_lowercase(self: Self) -> PandasLikeExpr:
117-
return reuse_series_namespace_implementation(
118-
self._compliant_expr,
119-
"str",
120-
"to_lowercase",
75+
return self._compliant_expr._reuse_series_namespace_implementation(
76+
"str", "to_lowercase"
12177
)

0 commit comments

Comments
 (0)