Skip to content

Commit 23ba1bd

Browse files
committed
move imports out of functions
1 parent df6208e commit 23ba1bd

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

python/datafusion/dataframe.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
from datafusion.plan import ExecutionPlan, LogicalPlan
3939
from datafusion.record_batch import RecordBatchStream
4040

41+
import pyarrow as pa
42+
from datafusion import functions as f
4143

4244
if TYPE_CHECKING:
4345
import pathlib
4446
from typing import Callable, Sequence
4547

4648
import pandas as pd
4749
import polars as pl
48-
import pyarrow as pa
4950

5051
from enum import Enum
5152

@@ -874,8 +875,6 @@ def fill_null(self, value: Any, subset: list[str] | None = None) -> "DataFrame":
874875
- For columns where casting fails, the original column is kept unchanged
875876
- For columns not in subset, the original column is kept unchanged
876877
"""
877-
import pyarrow as pa
878-
from datafusion import functions as f
879878

880879
# Get columns to process
881880
if subset is None:
@@ -910,38 +909,39 @@ def fill_null(self, value: Any, subset: list[str] | None = None) -> "DataFrame":
910909
exprs.append(f.col(col_name))
911910

912911
return self.select(*exprs)
913-
914-
def fill_nan(self, value: float | int, subset: list[str] | None = None) -> "DataFrame":
912+
913+
def fill_nan(
914+
self, value: float | int, subset: list[str] | None = None
915+
) -> "DataFrame":
915916
"""Fill NaN values in specified numeric columns with a value.
916-
917+
917918
Args:
918919
value: Numeric value to replace NaN values with
919920
subset: Optional list of column names to fill. If None, fills all numeric columns.
920-
921+
921922
Returns:
922923
DataFrame with NaN values replaced in numeric columns
923-
924+
924925
Examples:
925926
>>> df = df.fill_nan(0) # Fill all NaNs with 0 in numeric columns
926927
>>> df = df.fill_nan(99.9, subset=["price", "score"]) # Fill specific columns
927-
928+
928929
Notes:
929930
- Only fills NaN values in numeric columns (float32, float64)
930931
- Non-numeric columns are kept unchanged
931932
- For columns not in subset, the original column is kept unchanged
932933
- Value must be numeric (int or float)
933934
"""
934-
import pyarrow as pa
935-
from datafusion import functions as f
936-
935+
937936
if not isinstance(value, (int, float)):
938937
raise ValueError("Value must be numeric (int or float)")
939938

940939
# Get columns to process
941940
if subset is None:
942941
# Only get numeric columns if no subset specified
943942
subset = [
944-
field.name for field in self.schema()
943+
field.name
944+
for field in self.schema()
945945
if pa.types.is_floating(field.type)
946946
]
947947
else:
@@ -962,5 +962,5 @@ def fill_nan(self, value: float | int, subset: list[str] | None = None) -> "Data
962962
else:
963963
# Keep columns not in subset unchanged
964964
exprs.append(f.col(col_name))
965-
965+
966966
return self.select(*exprs)

python/tests/test_dataframe.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ def test_fill_null(df):
12641264
)
12651265
with pytest.raises(ValueError, match="Column 'f' not found in DataFrame"):
12661266
df_with_nulls.fill_null("missing", subset=["e", "f"])
1267-
1267+
12681268
def test_fill_nan(df):
12691269
# Test filling NaNs with integer value
12701270
df_with_nans = df.with_column("d", literal(float("nan")).cast(pa.float64()))
@@ -1311,4 +1311,4 @@ def test_fill_nan(df):
13111311
df_filled = df_with_nans.fill_nan(99.9, subset=["e"])
13121312
result = df_filled.to_pydict()
13131313
assert result["d"] == [float("nan"), float("nan"), float("nan")]
1314-
assert result["e"] == [99.9, 99.9, 99.9]
1314+
assert result["e"] == [99.9, 99.9, 99.9]

0 commit comments

Comments
 (0)