Skip to content

Commit

Permalink
even more, muaahahahahaha
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 7, 2025
1 parent f6c6af5 commit 676008f
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 375 deletions.
20 changes: 5 additions & 15 deletions narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,7 @@ def zip_with(self: Self, mask: Self, other: Self) -> Self:

mask = mask._native_series.combine_chunks()
return self._from_native_series(
pc.if_else(
mask,
self._native_series,
other._native_series,
)
pc.if_else(mask, self._native_series, other._native_series)
)

def sample(
Expand Down Expand Up @@ -689,10 +685,7 @@ def fill_aux(
)[::-1]
distance = valid_index - indices
return pc.if_else(
pc.and_(
pc.is_null(arr),
pc.less_equal(distance, pa.scalar(limit)),
),
pc.and_(pc.is_null(arr), pc.less_equal(distance, pa.scalar(limit))),
arr.take(valid_index),
arr,
)
Expand Down Expand Up @@ -1499,10 +1492,7 @@ def strip_chars(self: Self, characters: str | None) -> ArrowSeries:

whitespace = " \t\n\r\v\f"
return self._compliant_series._from_native_series(
pc.utf8_trim(
self._compliant_series._native_series,
characters or whitespace,
)
pc.utf8_trim(self._compliant_series._native_series, characters or whitespace)
)

def starts_with(self: Self, prefix: str) -> ArrowSeries:
Expand Down Expand Up @@ -1551,14 +1541,14 @@ def to_uppercase(self: Self) -> ArrowSeries:
import pyarrow.compute as pc

return self._compliant_series._from_native_series(
pc.utf8_upper(self._compliant_series._native_series),
pc.utf8_upper(self._compliant_series._native_series)
)

def to_lowercase(self: Self) -> ArrowSeries:
import pyarrow.compute as pc

return self._compliant_series._from_native_series(
pc.utf8_lower(self._compliant_series._native_series),
pc.utf8_lower(self._compliant_series._native_series)
)


Expand Down
13 changes: 2 additions & 11 deletions narwhals/_arrow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,7 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> pa
if isinstance_or_issubclass(dtype, dtypes.Struct):
return pa.struct(
[
(
field.name,
narwhals_to_native_dtype(
field.dtype,
version=version,
),
)
(field.name, narwhals_to_native_dtype(field.dtype, version=version))
for field in dtype.fields # type: ignore[union-attr]
]
)
Expand Down Expand Up @@ -315,10 +309,7 @@ def floordiv_compat(left: Any, right: Any) -> Any:
pc.bit_wise_xor(left, right), pa.scalar(0, type=divided.type)
)
result = pc.if_else(
pc.and_(
has_remainder,
has_one_negative_operand,
),
pc.and_(has_remainder, has_one_negative_operand),
# GH: 55561 ruff: ignore
pc.subtract(divided, pa.scalar(1, type=divided.type)),
divided,
Expand Down
5 changes: 1 addition & 4 deletions narwhals/_dask/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ def join(
)
return self._from_native_frame(
self._native_frame.merge(
other_native,
how="inner",
left_on=left_on,
right_on=left_on,
other_native, how="inner", left_on=left_on, right_on=left_on
)
)

Expand Down
3 changes: 1 addition & 2 deletions narwhals/_ibis/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def native_to_narwhals_dtype(ibis_dtype: Any, version: Version) -> DType:
return dtypes.Struct(
[
dtypes.Field(
ibis_dtype_name,
native_to_narwhals_dtype(ibis_dtype_field, version),
ibis_dtype_name, native_to_narwhals_dtype(ibis_dtype_field, version)
)
for ibis_dtype_name, ibis_dtype_field in ibis_dtype.items()
]
Expand Down
9 changes: 2 additions & 7 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,7 @@ def join(
else:
return self._from_native_frame(
self._native_frame.merge(
other._native_frame,
how="cross",
suffixes=("", suffix),
other._native_frame, how="cross", suffixes=("", suffix)
)
)

Expand Down Expand Up @@ -589,10 +587,7 @@ def join(
)
return self._from_native_frame(
self._native_frame.merge(
other_native,
how="inner",
left_on=left_on,
right_on=left_on,
other_native, how="inner", left_on=left_on, right_on=left_on
)
)

Expand Down
4 changes: 1 addition & 3 deletions narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,7 @@ class PandasLikeExprStringNamespace:
def __init__(self, expr: PandasLikeExpr) -> None:
self._compliant_expr = expr

def len_chars(
self,
) -> PandasLikeExpr:
def len_chars(self) -> PandasLikeExpr:
return reuse_series_namespace_implementation(
self._compliant_expr, "str", "len_chars"
)
Expand Down
31 changes: 14 additions & 17 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ def _from_iterable(
) -> Self:
return cls(
native_series_from_iterable(
data,
name=name,
index=index,
implementation=implementation,
data, name=name, index=index, implementation=implementation
),
implementation=implementation,
backend_version=backend_version,
Expand Down Expand Up @@ -1195,17 +1192,17 @@ def replace_all(

def strip_chars(self, characters: str | None) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.str.strip(characters),
self._compliant_series._native_series.str.strip(characters)
)

def starts_with(self, prefix: str) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.str.startswith(prefix),
self._compliant_series._native_series.str.startswith(prefix)
)

def ends_with(self, suffix: str) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.str.endswith(suffix),
self._compliant_series._native_series.str.endswith(suffix)
)

def contains(self, pattern: str, *, literal: bool = False) -> PandasLikeSeries:
Expand All @@ -1230,12 +1227,12 @@ def to_datetime(self: Self, format: str | None) -> PandasLikeSeries: # noqa: A0

def to_uppercase(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.str.upper(),
self._compliant_series._native_series.str.upper()
)

def to_lowercase(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.str.lower(),
self._compliant_series._native_series.str.lower()
)


Expand All @@ -1245,7 +1242,7 @@ def __init__(self, series: PandasLikeSeries) -> None:

def date(self) -> PandasLikeSeries:
result = self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.date,
self._compliant_series._native_series.dt.date
)
if str(result.dtype).lower() == "object":
msg = (
Expand All @@ -1260,32 +1257,32 @@ def date(self) -> PandasLikeSeries:

def year(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.year,
self._compliant_series._native_series.dt.year
)

def month(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.month,
self._compliant_series._native_series.dt.month
)

def day(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.day,
self._compliant_series._native_series.dt.day
)

def hour(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.hour,
self._compliant_series._native_series.dt.hour
)

def minute(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.minute,
self._compliant_series._native_series.dt.minute
)

def second(self) -> PandasLikeSeries:
return self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.second,
self._compliant_series._native_series.dt.second
)

def millisecond(self) -> PandasLikeSeries:
Expand Down Expand Up @@ -1335,7 +1332,7 @@ def ordinal_day(self) -> PandasLikeSeries:
def weekday(self) -> PandasLikeSeries:
return (
self._compliant_series._from_native_series(
self._compliant_series._native_series.dt.weekday,
self._compliant_series._native_series.dt.weekday
)
+ 1 # Pandas is 0-6 while Polars is 1-7
)
Expand Down
5 changes: 1 addition & 4 deletions narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,7 @@ def narwhals_to_native_dtype( # noqa: PLR0915
[
(
field.name,
arrow_narwhals_to_native_dtype(
field.dtype,
version=version,
),
arrow_narwhals_to_native_dtype(field.dtype, version=version),
)
for field in dtype.fields # type: ignore[union-attr]
]
Expand Down
10 changes: 2 additions & 8 deletions narwhals/_polars/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ def rolling_var(

return self._from_native_expr(
self._native_expr.rolling_var(
window_size=window_size,
min_periods=min_periods,
center=center,
ddof=ddof,
window_size=window_size, min_periods=min_periods, center=center, ddof=ddof
)
)

Expand All @@ -113,10 +110,7 @@ def rolling_std(

return self._from_native_expr(
self._native_expr.rolling_std(
window_size=window_size,
min_periods=min_periods,
center=center,
ddof=ddof,
window_size=window_size, min_periods=min_periods, center=center, ddof=ddof
)
)

Expand Down
6 changes: 1 addition & 5 deletions narwhals/_polars/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,7 @@ def concat_str(
)

return PolarsExpr(
pl.concat_str(
pl_exprs,
separator=separator,
ignore_nulls=ignore_nulls,
),
pl.concat_str(pl_exprs, separator=separator, ignore_nulls=ignore_nulls),
version=self._version,
backend_version=self._backend_version,
)
Expand Down
10 changes: 2 additions & 8 deletions narwhals/_polars/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ def rolling_var(

return self._from_native_series(
self._native_series.rolling_var(
window_size=window_size,
min_periods=min_periods,
center=center,
ddof=ddof,
window_size=window_size, min_periods=min_periods, center=center, ddof=ddof
)
)

Expand All @@ -324,10 +321,7 @@ def rolling_std(

return self._from_native_series(
self._native_series.rolling_std(
window_size=window_size,
min_periods=min_periods,
center=center,
ddof=ddof,
window_size=window_size, min_periods=min_periods, center=center, ddof=ddof
)
)

Expand Down
3 changes: 1 addition & 2 deletions narwhals/_polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> pl
return pl.Struct(
fields=[
pl.Field(
name=field.name,
dtype=narwhals_to_native_dtype(field.dtype, version),
name=field.name, dtype=narwhals_to_native_dtype(field.dtype, version)
)
for field in dtype.fields # type: ignore[union-attr]
]
Expand Down
5 changes: 1 addition & 4 deletions narwhals/_spark_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ def native_to_narwhals_dtype(
return dtypes.Boolean()
if isinstance(dtype, pyspark_types.DateType):
return dtypes.Date()
datetime_types = [
pyspark_types.TimestampType,
pyspark_types.TimestampNTZType,
]
datetime_types = [pyspark_types.TimestampType, pyspark_types.TimestampNTZType]
if any(isinstance(dtype, t) for t in datetime_types):
return dtypes.Datetime()
if isinstance(dtype, pyspark_types.DecimalType): # pragma: no cover
Expand Down
Loading

0 comments on commit 676008f

Please sign in to comment.