Skip to content

Commit fd759b3

Browse files
committed
'typing' pandas-like, adding comment
1 parent 3552f91 commit fd759b3

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎narwhals/_dask/expr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __narwhals_namespace__(self) -> DaskNamespace: # pragma: no cover
8585
def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Self:
8686
def func(df: DaskLazyFrame) -> list[dx.Series]:
8787
# result.loc[0][0] is a workaround for dask~<=2024.10.0/dask_expr~<=1.1.16
88-
# that raised a KeyErrror for result[0] during collection.
88+
# that raised a KeyError for result[0] during collection.
8989
return [result.loc[0][0] for result in self(df)]
9090

9191
return self.__class__(

‎narwhals/_pandas_like/series.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,14 @@ def __rtruediv__(self, other: Any) -> Self:
490490

491491
def __floordiv__(self, other: Any) -> Self:
492492
native, other = align_and_extract_native(self, other)
493+
native = cast("pd.Series[Any]", native)
493494
native_cls = type(native)
494495
other_is_series = isinstance(other, native_cls)
495496

496497
safe_div_mask = other != 0
497498

498-
if ((not other_is_series) and safe_div_mask is True) or (
499-
other_is_series and safe_div_mask.all()
499+
if (other_is_series and safe_div_mask.all()) or (
500+
(not other_is_series) and safe_div_mask is True
500501
): # fast path, there are no zero's in other
501502
result_native = native // other
502503
else:
@@ -511,10 +512,19 @@ def __floordiv__(self, other: Any) -> Self:
511512

512513
from narwhals._arrow.utils import floordiv_compat
513514

514-
native_pa = native.array._pa_array
515-
other_pa = other.array._pa_array if other_is_series else pa.scalar(other)
515+
native_pa = native.array._pa_array # type: ignore[attr-defined]
516+
other_pa = (
517+
other.array._pa_array # pyright: ignore[reportAttributeAccessIssue]
518+
if other_is_series
519+
else pa.scalar(other, type=pa.int64())
520+
# TODO(FBruzzesi): How can be improve on pinning down the type?
521+
)
516522
result_array = floordiv_compat(native_pa, other_pa)
517523
else:
524+
# Note that we use numpy/cupy where instead of pandas-like Series.where
525+
# to avoid handling broadcasting for scalars.
526+
# np.where(True, x, y) -> x, np.where(False, x, y) -> y
527+
# x.where(True, y) -> raises an exception due to shape mismatch
518528
result_array = array_funcs.where(
519529
safe_div_mask,
520530
native // other,

‎tests/stable_v1_dependencies_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas as pd
77

88

9-
def test_stable_v1_dependenies_import() -> None:
9+
def test_stable_v1_dependencies_import() -> None:
1010
# https://github.com/bokeh/bokeh/pull/14530#issuecomment-2984474111
1111
# Note: for some reason, this test needs to be in its own file to reproduce
1212
# the error observed in Bokeh's CI. Please don't move this test to

0 commit comments

Comments
 (0)