Skip to content

Commit

Permalink
move some imports deeper (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Dec 7, 2024
1 parent af1478b commit 9e8cdee
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 205 deletions.
24 changes: 12 additions & 12 deletions narwhals/_arrow/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def select(
*exprs: IntoArrowExpr,
**named_exprs: IntoArrowExpr,
) -> Self:
import pyarrow as pa # ignore-banned-import()
import pyarrow as pa

new_series = evaluate_into_exprs(self, *exprs, **named_exprs)
if not new_series:
Expand Down Expand Up @@ -473,7 +473,7 @@ def to_dict(
return {name: col.to_pylist() for name, col in names_and_values}

def with_row_index(self: Self, name: str) -> Self:
import pyarrow as pa # ignore-banned-import()
import pyarrow as pa

df = self._native_frame

Expand All @@ -500,7 +500,7 @@ def filter(self: Self, *predicates: IntoArrowExpr, **constraints: Any) -> Self:
return self._from_native_frame(self._native_frame.filter(mask))

def null_count(self: Self) -> Self:
import pyarrow as pa # ignore-banned-import()
import pyarrow as pa

df = self._native_frame
names_and_values = zip(df.column_names, df.columns)
Expand Down Expand Up @@ -572,12 +572,12 @@ def rename(self: Self, mapping: dict[str, str]) -> Self:
return self._from_native_frame(df.rename_columns(new_cols))

def write_parquet(self: Self, file: Any) -> None:
import pyarrow.parquet as pp # ignore-banned-import
import pyarrow.parquet as pp

pp.write_table(self._native_frame, file)

def write_csv(self: Self, file: Any) -> Any:
import pyarrow as pa # ignore-banned-import
import pyarrow as pa
import pyarrow.csv as pa_csv # ignore-banned-import

pa_table = self._native_frame
Expand All @@ -589,8 +589,8 @@ def write_csv(self: Self, file: Any) -> Any:

def is_duplicated(self: Self) -> ArrowSeries:
import numpy as np # ignore-banned-import
import pyarrow as pa # ignore-banned-import()
import pyarrow.compute as pc # ignore-banned-import()
import pyarrow as pa
import pyarrow.compute as pc

from narwhals._arrow.series import ArrowSeries

Expand All @@ -617,7 +617,7 @@ def is_duplicated(self: Self) -> ArrowSeries:
)

def is_unique(self: Self) -> ArrowSeries:
import pyarrow.compute as pc # ignore-banned-import()
import pyarrow.compute as pc

from narwhals._arrow.series import ArrowSeries

Expand All @@ -640,8 +640,8 @@ def unique(
# The param `maintain_order` is only here for compatibility with the Polars API
# and has no effect on the output.
import numpy as np # ignore-banned-import
import pyarrow as pa # ignore-banned-import()
import pyarrow.compute as pc # ignore-banned-import()
import pyarrow as pa
import pyarrow.compute as pc

df = self._native_frame

Expand Down Expand Up @@ -681,7 +681,7 @@ def sample(
seed: int | None,
) -> Self:
import numpy as np # ignore-banned-import
import pyarrow.compute as pc # ignore-banned-import()
import pyarrow.compute as pc

frame = self._native_frame
num_rows = len(self)
Expand All @@ -701,7 +701,7 @@ def unpivot(
variable_name: str | None,
value_name: str | None,
) -> Self:
import pyarrow as pa # ignore-banned-import
import pyarrow as pa

native_frame = self._native_frame
variable_name = variable_name if variable_name is not None else "variable"
Expand Down
10 changes: 5 additions & 5 deletions narwhals/_arrow/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_function_name_option(
function_name: str,
) -> pc.CountOptions | pc.VarianceOptions | None:
"""Map specific pyarrow compute function to respective option to match polars behaviour."""
import pyarrow.compute as pc # ignore-banned-import
import pyarrow.compute as pc

function_name_to_options = {
"count": pc.CountOptions(mode="all"),
Expand All @@ -48,7 +48,7 @@ class ArrowGroupBy:
def __init__(
self: Self, df: ArrowDataFrame, keys: list[str], *, drop_null_keys: bool
) -> None:
import pyarrow as pa # ignore-banned-import()
import pyarrow as pa

if drop_null_keys:
self._df = df.drop_nulls(keys)
Expand Down Expand Up @@ -87,8 +87,8 @@ def agg(
)

def __iter__(self: Self) -> Iterator[tuple[Any, ArrowDataFrame]]:
import pyarrow as pa # ignore-banned-import
import pyarrow.compute as pc # ignore-banned-import
import pyarrow as pa
import pyarrow.compute as pc

col_token = generate_temporary_column_name(n_bytes=8, columns=self._df.columns)
null_token = "__null_token_value__" # noqa: S105
Expand Down Expand Up @@ -127,7 +127,7 @@ def agg_arrow(
output_names: list[str],
from_dataframe: Callable[[Any], ArrowDataFrame],
) -> ArrowDataFrame:
import pyarrow.compute as pc # ignore-banned-import()
import pyarrow.compute as pc

all_simple_aggs = True
for expr in exprs:
Expand Down
12 changes: 6 additions & 6 deletions narwhals/_arrow/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _create_series_from_scalar(
)

def _create_compliant_series(self: Self, value: Any) -> ArrowSeries:
import pyarrow as pa # ignore-banned-import()
import pyarrow as pa

from narwhals._arrow.series import ArrowSeries

Expand Down Expand Up @@ -254,7 +254,7 @@ def func(df: ArrowDataFrame) -> list[ArrowSeries]:
)

def min_horizontal(self: Self, *exprs: IntoArrowExpr) -> ArrowExpr:
import pyarrow.compute as pc # ignore-banned-import
import pyarrow.compute as pc

parsed_exprs = parse_into_exprs(*exprs, namespace=self)

Expand Down Expand Up @@ -282,7 +282,7 @@ def func(df: ArrowDataFrame) -> list[ArrowSeries]:
)

def max_horizontal(self: Self, *exprs: IntoArrowExpr) -> ArrowExpr:
import pyarrow.compute as pc # ignore-banned-import
import pyarrow.compute as pc

parsed_exprs = parse_into_exprs(*exprs, namespace=self)

Expand Down Expand Up @@ -385,7 +385,7 @@ def concat_str(
separator: str,
ignore_nulls: bool,
) -> ArrowExpr:
import pyarrow.compute as pc # ignore-banned-import
import pyarrow.compute as pc

parsed_exprs: list[ArrowExpr] = [
*parse_into_exprs(*exprs, namespace=self),
Expand Down Expand Up @@ -438,8 +438,8 @@ def __init__(
self._version = version

def __call__(self: Self, df: ArrowDataFrame) -> list[ArrowSeries]:
import pyarrow as pa # ignore-banned-import
import pyarrow.compute as pc # ignore-banned-import
import pyarrow as pa
import pyarrow.compute as pc

from narwhals._arrow.namespace import ArrowNamespace
from narwhals._expression_parsing import parse_into_expr
Expand Down
Loading

0 comments on commit 9e8cdee

Please sign in to comment.