@@ -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 ,
0 commit comments