From 31dd90f7640924aff7f8502c329e71b9a6fa7352 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Mon, 27 Jan 2025 16:10:00 +0100 Subject: [PATCH] rm imports --- narwhals/_duckdb/dataframe.py | 4 ---- narwhals/_polars/dataframe.py | 5 ----- narwhals/_polars/expr.py | 6 ------ narwhals/_polars/namespace.py | 10 ---------- narwhals/_polars/series.py | 14 -------------- narwhals/_polars/utils.py | 2 -- narwhals/_spark_like/utils.py | 2 -- 7 files changed, 43 deletions(-) diff --git a/narwhals/_duckdb/dataframe.py b/narwhals/_duckdb/dataframe.py index c34028e84..bc7bf081f 100644 --- a/narwhals/_duckdb/dataframe.py +++ b/narwhals/_duckdb/dataframe.py @@ -315,8 +315,6 @@ def collect_schema(self: Self) -> dict[str, DType]: def unique(self: Self, subset: Sequence[str] | None, keep: str) -> Self: if subset is not None: - import duckdb - rel = self._native_frame # Sanitise input if any(x not in rel.columns for x in subset): @@ -365,8 +363,6 @@ def sort( return self._from_native_frame(result) def drop_nulls(self: Self, subset: list[str] | None) -> Self: - import duckdb - rel = self._native_frame subset_ = subset if subset is not None else rel.columns keep_condition = " and ".join(f'"{col}" is not null' for col in subset_) diff --git a/narwhals/_polars/dataframe.py b/narwhals/_polars/dataframe.py index 727912f11..0473a171a 100644 --- a/narwhals/_polars/dataframe.py +++ b/narwhals/_polars/dataframe.py @@ -101,8 +101,6 @@ def _from_native_object( def __getattr__(self: Self, attr: str) -> Any: def func(*args: Any, **kwargs: Any) -> Any: - import polars as pl - args, kwargs = extract_args_kwargs(args, kwargs) # type: ignore[assignment] try: return self._from_native_object( @@ -176,7 +174,6 @@ def __getitem__(self: Self, item: Any) -> Any: ) msg = f"Expected slice of integers or strings, got: {type(item[1])}" # pragma: no cover raise TypeError(msg) # pragma: no cover - import polars as pl if ( isinstance(item, tuple) @@ -412,8 +409,6 @@ def collect_schema(self: Self) -> dict[str, DType]: } def collect(self: Self) -> PolarsDataFrame: - import polars as pl - try: result = self._native_frame.collect() except pl.exceptions.ColumnNotFoundError as e: diff --git a/narwhals/_polars/expr.py b/narwhals/_polars/expr.py index 233b281cf..2b6969a3d 100644 --- a/narwhals/_polars/expr.py +++ b/narwhals/_polars/expr.py @@ -79,8 +79,6 @@ def ewm_mean( **extra_kwargs, ) if self._backend_version < (1,): # pragma: no cover - import polars as pl - return self._from_native_expr( pl.when(~expr.is_null()).then(native_expr).otherwise(None) ) @@ -352,14 +350,10 @@ def len(self: Self) -> PolarsExpr: native_result = native_expr.list.len() if self._expr._backend_version < (1, 16): # pragma: no cover - import polars as pl - native_result: pl.Expr = ( # type: ignore[no-redef] pl.when(~native_expr.is_null()).then(native_result).cast(pl.UInt32()) ) elif self._expr._backend_version < (1, 17): # pragma: no cover - import polars as pl - native_result = native_result.cast(pl.UInt32()) return self._expr._from_native_expr(native_result) diff --git a/narwhals/_polars/namespace.py b/narwhals/_polars/namespace.py index 33fb84dfc..60789862c 100644 --- a/narwhals/_polars/namespace.py +++ b/narwhals/_polars/namespace.py @@ -225,8 +225,6 @@ def by_dtype(self: Self, dtypes: Iterable[DType]) -> PolarsExpr: ) def numeric(self: Self) -> PolarsExpr: - import polars as pl - from narwhals._polars.expr import PolarsExpr return PolarsExpr( @@ -236,8 +234,6 @@ def numeric(self: Self) -> PolarsExpr: ) def boolean(self: Self) -> PolarsExpr: - import polars as pl - from narwhals._polars.expr import PolarsExpr return PolarsExpr( @@ -247,8 +243,6 @@ def boolean(self: Self) -> PolarsExpr: ) def string(self: Self) -> PolarsExpr: - import polars as pl - from narwhals._polars.expr import PolarsExpr return PolarsExpr( @@ -258,8 +252,6 @@ def string(self: Self) -> PolarsExpr: ) def categorical(self: Self) -> PolarsExpr: - import polars as pl - from narwhals._polars.expr import PolarsExpr return PolarsExpr( @@ -269,8 +261,6 @@ def categorical(self: Self) -> PolarsExpr: ) def all(self: Self) -> PolarsExpr: - import polars as pl - from narwhals._polars.expr import PolarsExpr return PolarsExpr( diff --git a/narwhals/_polars/series.py b/narwhals/_polars/series.py index 38e84f3f9..3fcc231bc 100644 --- a/narwhals/_polars/series.py +++ b/narwhals/_polars/series.py @@ -77,8 +77,6 @@ def _from_native_object(self: Self, series: T) -> T: ... def _from_native_object( self: Self, series: pl.Series | pl.DataFrame | T ) -> Self | PolarsDataFrame | T: - import polars as pl - if isinstance(series, pl.Series): return self._from_native_series(series) if isinstance(series, pl.DataFrame): @@ -244,8 +242,6 @@ def median(self: Self) -> Any: return self._native_series.median() def to_dummies(self: Self, *, separator: str, drop_first: bool) -> PolarsDataFrame: - import polars as pl - from narwhals._polars.dataframe import PolarsDataFrame if self._backend_version < (0, 20, 15): @@ -294,8 +290,6 @@ def ewm_mean( **extra_kwargs, ) if self._backend_version < (1,): # pragma: no cover - import polars as pl - return self._from_native_series( pl.select( pl.when(~native_series.is_null()).then(native_result).otherwise(None) @@ -405,8 +399,6 @@ def sort(self: Self, *, descending: bool, nulls_last: bool) -> Self: result = self._native_series.sort(descending=descending) if nulls_last: - import polars as pl - is_null = result.is_null() result = pl.concat([result.filter(~is_null), result.filter(is_null)]) else: @@ -433,8 +425,6 @@ def value_counts( from narwhals._polars.dataframe import PolarsDataFrame if self._backend_version < (1, 0, 0): - import polars as pl - value_name_ = name or ("proportion" if normalize else "count") result = self._native_series.value_counts(sort=sort, parallel=parallel) @@ -547,15 +537,11 @@ def len(self: Self) -> PolarsSeries: native_result = native_series.list.len() if self._series._backend_version < (1, 16): # pragma: no cover - import polars as pl - native_result = pl.select( pl.when(~native_series.is_null()).then(native_result).otherwise(None) )[native_series.name].cast(pl.UInt32()) elif self._series._backend_version < (1, 17): # pragma: no cover - import polars as pl - native_result = native_series.cast(pl.UInt32()) return self._series._from_native_series(native_result) diff --git a/narwhals/_polars/utils.py b/narwhals/_polars/utils.py index a69818adc..f0ee621bf 100644 --- a/narwhals/_polars/utils.py +++ b/narwhals/_polars/utils.py @@ -145,8 +145,6 @@ def native_to_narwhals_dtype( def narwhals_to_native_dtype( dtype: DType | type[DType], version: Version, backend_version: tuple[int, ...] ) -> pl.DataType: - import polars as pl - dtypes = import_dtypes_module(version) if dtype == dtypes.Float64: diff --git a/narwhals/_spark_like/utils.py b/narwhals/_spark_like/utils.py index aa3ccce0e..b0a613e8a 100644 --- a/narwhals/_spark_like/utils.py +++ b/narwhals/_spark_like/utils.py @@ -15,8 +15,6 @@ from narwhals.utils import isinstance_or_issubclass if TYPE_CHECKING: - from pyspark.sql import Column - from narwhals._spark_like.dataframe import SparkLikeLazyFrame from narwhals._spark_like.expr import SparkLikeExpr from narwhals.dtypes import DType