diff --git a/narwhals/_arrow/series.py b/narwhals/_arrow/series.py index 5f78dfdbfa..a7ffd2f027 100644 --- a/narwhals/_arrow/series.py +++ b/narwhals/_arrow/series.py @@ -191,11 +191,11 @@ def __narwhals_namespace__(self: Self) -> ArrowNamespace: def __eq__(self: Self, other: object) -> Self: # type: ignore[override] ser, other = extract_native(self, other) - return self._with_native(pc.equal(ser, other)) # type: ignore[arg-type] + return self._with_native(pc.equal(ser, other)) # type: ignore[call-overload] def __ne__(self: Self, other: object) -> Self: # type: ignore[override] ser, other = extract_native(self, other) - return self._with_native(pc.not_equal(ser, other)) # type: ignore[arg-type] + return self._with_native(pc.not_equal(ser, other)) # type: ignore[call-overload] def __ge__(self: Self, other: Any) -> Self: ser, other = extract_native(self, other) @@ -271,14 +271,14 @@ def __truediv__(self: Self, other: Any) -> Self: if not isinstance(other, (pa.Array, pa.ChunkedArray)): # scalar other = lit(other) - return self._with_native(pc.divide(*cast_for_truediv(ser, other))) + return self._with_native(pc.divide(*cast_for_truediv(ser, other))) # type: ignore[arg-type, type-var] def __rtruediv__(self: Self, other: Any) -> Self: ser, other = extract_native(self, other) if not isinstance(other, (pa.Array, pa.ChunkedArray)): # scalar other = lit(other) if not isinstance(other, pa.Scalar) else other - return self._with_native(pc.divide(*cast_for_truediv(other, ser))) # pyright: ignore[reportArgumentType] + return self._with_native(pc.divide(*cast_for_truediv(other, ser))) # type: ignore[arg-type, type-var] def __mod__(self: Self, other: Any) -> Self: floor_div = (self // other).native @@ -596,11 +596,11 @@ def value_counts( counts = cast("ArrowChunkedArray", val_counts.field("counts")) if normalize: - arrays = [values, pc.divide(*cast_for_truediv(counts, pc.sum(counts)))] + arrays = [values, pc.divide(*cast_for_truediv(counts, pc.sum(counts)))] # type: ignore[type-var] else: arrays = [values, counts] - val_count = pa.Table.from_arrays(arrays, names=[index_name_, value_name_]) + val_count = pa.Table.from_arrays(arrays, names=[index_name_, value_name_]) # type: ignore[arg-type] if sort: val_count = val_count.sort_by([(value_name_, "descending")]) @@ -662,7 +662,7 @@ def fill_aux( )[::-1] distance = valid_index - indices return pc.if_else( - pc.and_(pc.is_null(arr), pc.less_equal(distance, lit(limit))), + pc.and_(pc.is_null(arr), pc.less_equal(distance, lit(limit))), # pyright: ignore[reportArgumentType, reportCallIssue] arr.take(valid_index), arr, ) @@ -1041,8 +1041,6 @@ def rank( ) raise ValueError(msg) - # ignore-banned-import - sort_keys: Order = "descending" if descending else "ascending" tiebreaker: TieBreaker = "first" if method == "ordinal" else method @@ -1078,7 +1076,7 @@ def _hist_from_bin_count(bin_count: int): # type: ignore[no-untyped-def] # noqa lower, upper = d["min"], d["max"] pa_float = pa.type_for_alias("float") if lower == upper: - range_ = lit(1.0) + range_: pa.Scalar[Any] = lit(1.0) mid = lit(0.5) width = pc.divide(range_, lit(bin_count)) lower = pc.subtract(lower, mid) @@ -1094,9 +1092,9 @@ def _hist_from_bin_count(bin_count: int): # type: ignore[no-untyped-def] # noqa bin_indices = pc.if_else( pc.and_( pc.equal(bin_indices, bin_proportions), - pc.greater(bin_indices, 0), + pc.greater(bin_indices, lit(0)), ), - pc.subtract(bin_indices, 1), + pc.subtract(bin_indices, lit(1)), bin_indices, ) possible = pa.Table.from_arrays( diff --git a/narwhals/_arrow/series_dt.py b/narwhals/_arrow/series_dt.py index eca801e162..4e1551fee0 100644 --- a/narwhals/_arrow/series_dt.py +++ b/narwhals/_arrow/series_dt.py @@ -16,7 +16,6 @@ from typing_extensions import Self from narwhals._arrow.series import ArrowSeries - from narwhals._arrow.typing import ArrowChunkedArray from narwhals.dtypes import Datetime from narwhals.typing import TimeUnit @@ -56,47 +55,48 @@ def timestamp(self: Self, time_unit: TimeUnit) -> ArrowSeries: s_cast = self.native.cast(pa.int64()) if unit == "ns": if time_unit == "ns": - result = s_cast + result_64 = s_cast elif time_unit == "us": - result = floordiv_compat(s_cast, 1_000) + result_64 = floordiv_compat(s_cast, 1_000) else: - result = floordiv_compat(s_cast, 1_000_000) + result_64 = floordiv_compat(s_cast, 1_000_000) elif unit == "us": if time_unit == "ns": - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000)) + result_64 = pc.multiply(s_cast, lit(1_000)) elif time_unit == "us": - result = s_cast + result_64 = s_cast else: - result = floordiv_compat(s_cast, 1_000) + result_64 = floordiv_compat(s_cast, 1_000) elif unit == "ms": if time_unit == "ns": - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000_000)) + result_64 = pc.multiply(s_cast, lit(1_000_000)) elif time_unit == "us": - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000)) + result_64 = pc.multiply(s_cast, lit(1_000)) else: - result = s_cast + result_64 = s_cast elif unit == "s": if time_unit == "ns": - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000_000_000)) + result_64 = pc.multiply(s_cast, lit(1_000_000_000)) elif time_unit == "us": - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000_000)) + result_64 = pc.multiply(s_cast, lit(1_000_000)) else: - result = cast("ArrowChunkedArray", pc.multiply(s_cast, 1_000)) + result_64 = pc.multiply(s_cast, lit(1_000)) else: # pragma: no cover msg = f"unexpected time unit {unit}, please report an issue at https://github.com/narwhals-dev/narwhals" raise AssertionError(msg) + return self.with_native(result_64) elif isinstance(ser.dtype, dtypes.Date): - time_s = pc.multiply(self.native.cast(pa.int32()), 86400) + time_s = pc.multiply(self.native.cast(pa.int32()), lit(86_400)) if time_unit == "ns": - result = cast("ArrowChunkedArray", pc.multiply(time_s, 1_000_000_000)) + result_32 = pc.multiply(time_s, lit(1_000_000_000)) elif time_unit == "us": - result = cast("ArrowChunkedArray", pc.multiply(time_s, 1_000_000)) + result_32 = pc.multiply(time_s, lit(1_000_000)) else: - result = cast("ArrowChunkedArray", pc.multiply(time_s, 1_000)) + result_32 = pc.multiply(time_s, lit(1_000)) + return self.with_native(result_32) else: msg = "Input should be either of Date or Datetime type" raise TypeError(msg) - return self.with_native(result) def date(self: Self) -> ArrowSeries: return self.with_native(self.native.cast(pa.date32())) diff --git a/narwhals/_pandas_like/series_dt.py b/narwhals/_pandas_like/series_dt.py index ae65c33e65..2fff762bef 100644 --- a/narwhals/_pandas_like/series_dt.py +++ b/narwhals/_pandas_like/series_dt.py @@ -58,10 +58,12 @@ def microsecond(self) -> PandasLikeSeries: # crazy workaround for https://github.com/pandas-dev/pandas/issues/59154 import pyarrow.compute as pc # ignore-banned-import() + from narwhals._arrow.utils import lit + arr_ns = self.native.array arr = arr_ns.__arrow_array__() result_arr = pc.add( - pc.multiply(pc.millisecond(arr), 1000), pc.microsecond(arr) + pc.multiply(pc.millisecond(arr), lit(1_000)), pc.microsecond(arr) ) result = type(self.native)(type(arr_ns)(result_arr), name=self.native.name) return self.with_native(result) diff --git a/pyproject.toml b/pyproject.toml index 57ae0f5b70..81b7611329 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,7 +67,7 @@ typing = [ "typing_extensions", "mypy~=1.15.0", "pyright", - "pyarrow-stubs==17.18", + "pyarrow-stubs==19.1", "sqlframe", "polars==1.25.2", "uv",