Skip to content

Commit 3da91c4

Browse files
committed
Merge remote-tracking branch 'upstream/main' into more-dedup-1
2 parents 819bf3b + b987e1a commit 3da91c4

File tree

21 files changed

+114
-38
lines changed

21 files changed

+114
-38
lines changed

docs/backcompat.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ before making any change.
113113

114114
The following are differences between the main Narwhals namespace and `narwhals.stable.v1`:
115115

116+
- Since Narwhals 1.29.0, `LazyFrame.gather_every` has been deprecated from the main namespace.
117+
116118
- Since Narwhals 1.24.1, an empty or all-null object-dtype pandas Series is inferred to
117119
be of dtype `String`. Previously, it would have been inferred as `Object`.
118120

narwhals/_arrow/dataframe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def drop(self: Self, columns: list[str], strict: bool) -> Self: # noqa: FBT001
469469
self._native_frame.drop(to_drop), validate_column_names=False
470470
)
471471

472-
def drop_nulls(self: Self, subset: list[str] | None) -> ArrowDataFrame:
472+
def drop_nulls(self: ArrowDataFrame, subset: list[str] | None) -> ArrowDataFrame:
473473
if subset is None:
474474
return self._from_native_frame(
475475
self._native_frame.drop_null(), validate_column_names=False
@@ -745,7 +745,7 @@ def is_unique(self: Self) -> ArrowSeries:
745745
)
746746

747747
def unique(
748-
self: Self,
748+
self: ArrowDataFrame,
749749
subset: list[str] | None,
750750
*,
751751
keep: Literal["any", "first", "last", "none"],

narwhals/_dask/dataframe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from narwhals._dask.expr import DaskExpr
3030
from narwhals._dask.group_by import DaskLazyGroupBy
3131
from narwhals._dask.namespace import DaskNamespace
32-
from narwhals._polars.dataframe import PolarsDataFrame
3332
from narwhals.dtypes import DType
3433
from narwhals.utils import Version
3534

@@ -90,7 +89,7 @@ def collect(
9089
self: Self,
9190
backend: Implementation | None,
9291
**kwargs: Any,
93-
) -> CompliantDataFrame[Any] | PolarsDataFrame:
92+
) -> CompliantDataFrame[Any]:
9493
import pandas as pd
9594

9695
result = self._native_frame.compute(**kwargs)

narwhals/_duckdb/dataframe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from narwhals._duckdb.group_by import DuckDBGroupBy
3636
from narwhals._duckdb.namespace import DuckDBNamespace
3737
from narwhals._duckdb.series import DuckDBInterchangeSeries
38-
from narwhals._polars.dataframe import PolarsDataFrame
3938
from narwhals.dtypes import DType
4039

4140
from narwhals.typing import CompliantLazyFrame
@@ -90,7 +89,7 @@ def collect(
9089
self: Self,
9190
backend: ModuleType | Implementation | str | None,
9291
**kwargs: Any,
93-
) -> CompliantDataFrame[Any] | PolarsDataFrame:
92+
) -> CompliantDataFrame[Any]:
9493
if backend is None or backend is Implementation.PYARROW:
9594
import pyarrow as pa # ignore-banned-import
9695

narwhals/_pandas_like/dataframe.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
from narwhals._pandas_like.expr import PandasLikeExpr
4949
from narwhals._pandas_like.group_by import PandasLikeGroupBy
5050
from narwhals._pandas_like.namespace import PandasLikeNamespace
51-
from narwhals._polars.dataframe import PolarsDataFrame
5251
from narwhals.dtypes import DType
5352
from narwhals.typing import SizeUnit
5453
from narwhals.typing import _1DArray
@@ -397,7 +396,9 @@ def simple_select(self: Self, *column_names: str) -> Self:
397396
validate_column_names=False,
398397
)
399398

400-
def aggregate(self: Self, *exprs: PandasLikeExpr) -> PandasLikeDataFrame:
399+
def aggregate(
400+
self: PandasLikeDataFrame, *exprs: PandasLikeExpr
401+
) -> PandasLikeDataFrame:
401402
return self.select(*exprs)
402403

403404
def select(self: PandasLikeDataFrame, *exprs: PandasLikeExpr) -> PandasLikeDataFrame:
@@ -415,7 +416,9 @@ def select(self: PandasLikeDataFrame, *exprs: PandasLikeExpr) -> PandasLikeDataF
415416
)
416417
return self._from_native_frame(df, validate_column_names=True)
417418

418-
def drop_nulls(self: Self, subset: list[str] | None) -> PandasLikeDataFrame:
419+
def drop_nulls(
420+
self: PandasLikeDataFrame, subset: list[str] | None
421+
) -> PandasLikeDataFrame:
419422
if subset is None:
420423
return self._from_native_frame(
421424
self._native_frame.dropna(axis=0), validate_column_names=False
@@ -533,7 +536,7 @@ def collect(
533536
self: Self,
534537
backend: Implementation | None,
535538
**kwargs: Any,
536-
) -> CompliantDataFrame[Any] | PolarsDataFrame:
539+
) -> CompliantDataFrame[Any]:
537540
if backend is None:
538541
return PandasLikeDataFrame(
539542
self._native_frame,

narwhals/_polars/dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def collect(
474474
self: Self,
475475
backend: Implementation | None,
476476
**kwargs: Any,
477-
) -> PolarsDataFrame | CompliantDataFrame[Any]:
477+
) -> CompliantDataFrame[Any]:
478478
try:
479479
result = self._native_frame.collect(**kwargs)
480480
except Exception as e: # noqa: BLE001

narwhals/_polars/series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ def dtype(self: Self) -> DType:
115115
self._native_series.dtype, self._version, self._backend_version
116116
)
117117

118+
def alias(self, name: str) -> Self:
119+
return self._from_native_object(self._native_series.alias(name))
120+
118121
@overload
119122
def __getitem__(self: Self, item: int) -> Any: ...
120123

narwhals/dataframe.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from narwhals.utils import flatten
3535
from narwhals.utils import generate_repr
3636
from narwhals.utils import is_sequence_but_not_str
37+
from narwhals.utils import issue_deprecation_warning
3738
from narwhals.utils import parse_version
3839

3940
if TYPE_CHECKING:
@@ -201,21 +202,22 @@ def filter(
201202
and isinstance(predicates[0], list)
202203
and all(isinstance(x, bool) for x in predicates[0])
203204
):
205+
from narwhals.functions import col
206+
204207
flat_predicates = flatten(predicates)
205208
check_expressions_preserve_length(*flat_predicates, function_name="filter")
206-
compliant_predicates, _kinds = self._flatten_and_extract(*flat_predicates)
207209
plx = self.__narwhals_namespace__()
210+
compliant_predicates, _kinds = self._flatten_and_extract(*flat_predicates)
211+
compliant_constraints = (
212+
(col(name) == v)._to_compliant_expr(plx)
213+
for name, v in constraints.items()
214+
)
208215
predicate = plx.all_horizontal(
209-
*chain(
210-
compliant_predicates,
211-
(plx.col(name) == v for name, v in constraints.items()),
212-
)
216+
*chain(compliant_predicates, compliant_constraints)
213217
)
214218
else:
215219
predicate = predicates[0]
216-
return self._from_compliant_dataframe(
217-
self._compliant_frame.filter(predicate),
218-
)
220+
return self._from_compliant_dataframe(self._compliant_frame.filter(predicate))
219221

220222
def sort(
221223
self: Self,
@@ -3140,13 +3142,25 @@ def lazy(self: Self) -> Self:
31403142
def gather_every(self: Self, n: int, offset: int = 0) -> Self:
31413143
r"""Take every nth row in the DataFrame and return as a new DataFrame.
31423144
3145+
!!! warning
3146+
`LazyFrame.gather_every` is deprecated and will be removed in a future version.
3147+
Note: this will remain available in `narwhals.stable.v1`.
3148+
See [stable api](../backcompat.md/) for more information.
3149+
31433150
Arguments:
31443151
n: Gather every *n*-th row.
31453152
offset: Starting index.
31463153
31473154
Returns:
31483155
The LazyFrame containing only the selected rows.
31493156
"""
3157+
msg = (
3158+
"`LazyFrame.gather_every` is deprecated and will be removed in a future version.\n\n"
3159+
"Note: this will remain available in `narwhals.stable.v1`.\n"
3160+
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
3161+
)
3162+
issue_deprecation_warning(msg, _version="1.29.0")
3163+
31503164
return super().gather_every(n=n, offset=offset)
31513165

31523166
def unpivot(

narwhals/expr.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ def sort(self: Self, *, descending: bool = False, nulls_last: bool = False) -> S
10961096
"Note: this will remain available in `narwhals.stable.v1`.\n"
10971097
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
10981098
)
1099-
issue_deprecation_warning(msg, _version="1.22.0")
1099+
issue_deprecation_warning(msg, _version="1.23.0")
11001100
return self.__class__(
11011101
lambda plx: self._to_compliant_expr(plx).sort(
11021102
descending=descending, nulls_last=nulls_last
@@ -1485,7 +1485,7 @@ def sample(
14851485
"Note: this will remain available in `narwhals.stable.v1`.\n"
14861486
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
14871487
)
1488-
issue_deprecation_warning(msg, _version="1.22.0")
1488+
issue_deprecation_warning(msg, _version="1.23.0")
14891489
return self.__class__(
14901490
lambda plx: self._to_compliant_expr(plx).sample(
14911491
n, fraction=fraction, with_replacement=with_replacement, seed=seed
@@ -1753,7 +1753,7 @@ def head(self: Self, n: int = 10) -> Self:
17531753
"Note: this will remain available in `narwhals.stable.v1`.\n"
17541754
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
17551755
)
1756-
issue_deprecation_warning(msg, _version="1.22.0")
1756+
issue_deprecation_warning(msg, _version="1.23.0")
17571757
return self.__class__(
17581758
lambda plx: self._to_compliant_expr(plx).head(n),
17591759
self._metadata.with_kind_and_extra_open_window(ExprKind.FILTRATION),
@@ -1781,7 +1781,7 @@ def tail(self: Self, n: int = 10) -> Self:
17811781
"Note: this will remain available in `narwhals.stable.v1`.\n"
17821782
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
17831783
)
1784-
issue_deprecation_warning(msg, _version="1.22.0")
1784+
issue_deprecation_warning(msg, _version="1.23.0")
17851785
return self.__class__(
17861786
lambda plx: self._to_compliant_expr(plx).tail(n),
17871787
self._metadata.with_kind_and_extra_open_window(ExprKind.FILTRATION),
@@ -1876,7 +1876,7 @@ def gather_every(self: Self, n: int, offset: int = 0) -> Self:
18761876
"Note: this will remain available in `narwhals.stable.v1`.\n"
18771877
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
18781878
)
1879-
issue_deprecation_warning(msg, _version="1.22.0")
1879+
issue_deprecation_warning(msg, _version="1.23.0")
18801880
return self.__class__(
18811881
lambda plx: self._to_compliant_expr(plx).gather_every(n=n, offset=offset),
18821882
self._metadata.with_kind_and_extra_open_window(ExprKind.FILTRATION),

narwhals/stable/v1/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ def tail(self, n: int = 5) -> Self: # pragma: no cover
348348
"""
349349
return super().tail(n)
350350

351+
def gather_every(self: Self, n: int, offset: int = 0) -> Self:
352+
r"""Take every nth row in the DataFrame and return as a new DataFrame.
353+
354+
Arguments:
355+
n: Gather every *n*-th row.
356+
offset: Starting index.
357+
358+
Returns:
359+
The LazyFrame containing only the selected rows.
360+
"""
361+
return self._from_compliant_dataframe(
362+
self._compliant_frame.gather_every(n=n, offset=offset)
363+
)
364+
351365

352366
class Series(NwSeries[IntoSeriesT]):
353367
"""Narwhals Series, backed by a native series.

0 commit comments

Comments
 (0)