Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")])
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
36 changes: 18 additions & 18 deletions narwhals/_arrow/series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()))
Expand Down
4 changes: 3 additions & 1 deletion narwhals/_pandas_like/series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading