Skip to content

Commit

Permalink
chore(python): Rename args f/func to function (pola-rs#7032)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored and josemasar committed Feb 21, 2023
1 parent 10a05c8 commit 34e1681
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 74 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ class Unknown(DataType):
T = TypeVar("T")


def cache(func: Callable[..., T]) -> T:
def cache(function: Callable[..., T]) -> T:
# need this to satisfy mypy issue with "@property/@cache combination"
# See: https://github.com/python/mypy/issues/5858
return functools.lru_cache()(func) # type: ignore[return-value]
return functools.lru_cache()(function) # type: ignore[return-value]


class _DataTypeMappings:
Expand Down
9 changes: 5 additions & 4 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4481,10 +4481,11 @@ def join(
._df
)

@deprecate_nonkeyword_arguments(allowed_args=["self", "f", "return_dtype"])
@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments(allowed_args=["self", "function", "return_dtype"])
def apply(
self,
f: Callable[[tuple[Any, ...]], Any],
function: Callable[[tuple[Any, ...]], Any],
return_dtype: PolarsDataType | None = None,
inference_size: int = 256,
) -> Self:
Expand All @@ -4507,7 +4508,7 @@ def apply(
Parameters
----------
f
function
Custom function/ lambda function.
return_dtype
Output type of the operation. If none given, Polars tries to infer the type.
Expand Down Expand Up @@ -4563,7 +4564,7 @@ def apply(
>>> df.select(pl.col("foo") * 2 + pl.col("bar")) # doctest: +IGNORE_RESULT
"""
out, is_df = self._df.apply(f, return_dtype, inference_size)
out, is_df = self._df.apply(function, return_dtype, inference_size)
if is_df:
return self._from_pydf(out)
else:
Expand Down
9 changes: 5 additions & 4 deletions py-polars/polars/internals/dataframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)

import polars.internals as pli
from polars.utils import _timedelta_to_pl_duration, redirect
from polars.utils import _timedelta_to_pl_duration, deprecated_alias, redirect

if TYPE_CHECKING:
from polars.internals.type_aliases import (
Expand Down Expand Up @@ -233,7 +233,8 @@ def agg(
)
return self.df.__class__._from_pydf(df._df)

def apply(self, f: Callable[[pli.DataFrame], pli.DataFrame]) -> DF:
@deprecated_alias(f="function")
def apply(self, function: Callable[[pli.DataFrame], pli.DataFrame]) -> DF:
"""
Apply a custom/user-defined function (UDF) over the groups as a sub-DataFrame.
Expand All @@ -251,7 +252,7 @@ def apply(self, f: Callable[[pli.DataFrame], pli.DataFrame]) -> DF:
Parameters
----------
f
function
Custom function.
Returns
Expand Down Expand Up @@ -320,7 +321,7 @@ def apply(self, f: Callable[[pli.DataFrame], pli.DataFrame]) -> DF:
raise TypeError("Cannot call `apply` when grouping by an expression.")

return self.df.__class__._from_pydf(
self.df._df.groupby_apply(by, f, self.maintain_order)
self.df._df.groupby_apply(by, function, self.maintain_order)
)

def head(self, n: int = 5) -> DF:
Expand Down
30 changes: 18 additions & 12 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from polars.utils import (
_timedelta_to_pl_duration,
deprecate_nonkeyword_arguments,
deprecated_alias,
sphinx_accessor,
)

Expand Down Expand Up @@ -774,13 +775,14 @@ def suffix(self, suffix: str) -> Self:
""" # noqa: W505
return self._from_pyexpr(self._pyexpr.suffix(suffix))

def map_alias(self, f: Callable[[str], str]) -> Self:
@deprecated_alias(f="function")
def map_alias(self, function: Callable[[str], str]) -> Self:
"""
Rename the output of an expression by mapping a function over the root name.
Parameters
----------
f
function
Function that maps root name to new name.
Examples
Expand All @@ -805,7 +807,7 @@ def map_alias(self, f: Callable[[str], str]) -> Self:
└───────────┴───────────┘
"""
return self._from_pyexpr(self._pyexpr.map_alias(f))
return self._from_pyexpr(self._pyexpr.map_alias(function))

def is_not(self) -> Self:
"""
Expand Down Expand Up @@ -3092,10 +3094,11 @@ def where(self, predicate: Expr) -> Self:
"""
return self.filter(predicate)

@deprecate_nonkeyword_arguments(allowed_args=["self", "f", "return_dtype"])
@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments(allowed_args=["self", "function", "return_dtype"])
def map(
self,
f: Callable[[pli.Series], pli.Series | Any],
function: Callable[[pli.Series], pli.Series | Any],
return_dtype: PolarsDataType | None = None,
agg_list: bool = False,
) -> Self:
Expand All @@ -3112,7 +3115,7 @@ def map(
Parameters
----------
f
function
Lambda/ function to apply.
return_dtype
Dtype of the output Series.
Expand Down Expand Up @@ -3140,12 +3143,13 @@ def map(
"""
if return_dtype is not None:
return_dtype = py_type_to_dtype(return_dtype)
return self._from_pyexpr(self._pyexpr.map(f, return_dtype, agg_list))
return self._from_pyexpr(self._pyexpr.map(function, return_dtype, agg_list))

@deprecate_nonkeyword_arguments(allowed_args=["self", "f", "return_dtype"])
@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments(allowed_args=["self", "function", "return_dtype"])
def apply(
self,
f: Callable[[pli.Series], pli.Series] | Callable[[Any], Any],
function: Callable[[pli.Series], pli.Series] | Callable[[Any], Any],
return_dtype: PolarsDataType | None = None,
skip_nulls: bool = True,
pass_name: bool = False,
Expand Down Expand Up @@ -3176,7 +3180,7 @@ def apply(
Parameters
----------
f
function
Lambda/ function to apply.
return_dtype
Dtype of the output Series.
Expand Down Expand Up @@ -3250,7 +3254,7 @@ def apply(
def wrap_f(x: pli.Series) -> pli.Series: # pragma: no cover
def inner(s: pli.Series) -> pli.Series: # pragma: no cover
s.rename(x.name, in_place=True)
return f(s)
return function(s)

return x.apply(inner, return_dtype=return_dtype, skip_nulls=skip_nulls)

Expand All @@ -3259,7 +3263,9 @@ def inner(s: pli.Series) -> pli.Series: # pragma: no cover
else:

def wrap_f(x: pli.Series) -> pli.Series: # pragma: no cover
return x.apply(f, return_dtype=return_dtype, skip_nulls=skip_nulls)
return x.apply(
function, return_dtype=return_dtype, skip_nulls=skip_nulls
)

return self.map(wrap_f, agg_list=True, return_dtype=return_dtype)

Expand Down
51 changes: 32 additions & 19 deletions py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_time_to_pl_time,
_timedelta_to_pl_timedelta,
deprecate_nonkeyword_arguments,
deprecated_alias,
)

try:
Expand Down Expand Up @@ -1497,9 +1498,10 @@ def cov(
return pli.wrap_expr(pycov(a._pyexpr, b._pyexpr))


@deprecated_alias(f="function")
def map(
exprs: Sequence[str] | Sequence[pli.Expr],
f: Callable[[Sequence[pli.Series]], pli.Series],
function: Callable[[Sequence[pli.Series]], pli.Series],
return_dtype: PolarsDataType | None = None,
) -> pli.Expr:
"""
Expand All @@ -1511,7 +1513,7 @@ def map(
----------
exprs
Input Series to f
f
function
Function to apply over the input
return_dtype
dtype of the output Series
Expand All @@ -1523,14 +1525,17 @@ def map(
"""
exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(
_map_mul(exprs, f, return_dtype, apply_groups=False, returns_scalar=False)
_map_mul(
exprs, function, return_dtype, apply_groups=False, returns_scalar=False
)
)


@deprecate_nonkeyword_arguments(allowed_args=["exprs", "f", "return_dtype"])
@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments(allowed_args=["exprs", "function", "return_dtype"])
def apply(
exprs: Sequence[str | pli.Expr],
f: Callable[[Sequence[pli.Series]], pli.Series | Any],
function: Callable[[Sequence[pli.Series]], pli.Series | Any],
return_dtype: PolarsDataType | None = None,
returns_scalar: bool = True,
) -> pli.Expr:
Expand All @@ -1549,7 +1554,7 @@ def apply(
----------
exprs
Input Series to f
f
function
Function to apply over the input
return_dtype
dtype of the output Series
Expand All @@ -1564,14 +1569,19 @@ def apply(
exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(
_map_mul(
exprs, f, return_dtype, apply_groups=True, returns_scalar=returns_scalar
exprs,
function,
return_dtype,
apply_groups=True,
returns_scalar=returns_scalar,
)
)


@deprecated_alias(f="function")
def fold(
acc: IntoExpr,
f: Callable[[pli.Series, pli.Series], pli.Series],
function: Callable[[pli.Series, pli.Series], pli.Series],
exprs: Sequence[pli.Expr | str] | pli.Expr,
) -> pli.Expr:
"""
Expand All @@ -1582,7 +1592,7 @@ def fold(
acc
Accumulator Expression. This is the value that will be initialized when the fold
starts. For a sum this could for instance be lit(0).
f
function
Function to apply over the accumulator and the value.
Fn(acc, value) -> new_value
exprs
Expand All @@ -1600,19 +1610,20 @@ def fold(
exprs = [exprs]

exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(pyfold(acc._pyexpr, f, exprs))
return pli.wrap_expr(pyfold(acc._pyexpr, function, exprs))


@deprecated_alias(f="function")
def reduce(
f: Callable[[pli.Series, pli.Series], pli.Series],
function: Callable[[pli.Series, pli.Series], pli.Series],
exprs: Sequence[pli.Expr | str] | pli.Expr,
) -> pli.Expr:
"""
Accumulate over multiple columns horizontally/ row wise with a left fold.
Parameters
----------
f
function
Function to apply over the accumulator and the value.
Fn(acc, value) -> new_value
exprs
Expand All @@ -1628,13 +1639,14 @@ def reduce(
exprs = [exprs]

exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(pyreduce(f, exprs))
return pli.wrap_expr(pyreduce(function, exprs))


@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments()
def cumfold(
acc: IntoExpr,
f: Callable[[pli.Series, pli.Series], pli.Series],
function: Callable[[pli.Series, pli.Series], pli.Series],
exprs: Sequence[pli.Expr | str] | pli.Expr,
include_init: bool = False,
) -> pli.Expr:
Expand All @@ -1648,7 +1660,7 @@ def cumfold(
acc
Accumulator Expression. This is the value that will be initialized when the fold
starts. For a sum this could for instance be lit(0).
f
function
Function to apply over the accumulator and the value.
Fn(acc, value) -> new_value
exprs
Expand All @@ -1668,11 +1680,12 @@ def cumfold(
exprs = [exprs]

exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(pycumfold(acc._pyexpr, f, exprs, include_init))
return pli.wrap_expr(pycumfold(acc._pyexpr, function, exprs, include_init))


@deprecated_alias(f="function")
def cumreduce(
f: Callable[[pli.Series, pli.Series], pli.Series],
function: Callable[[pli.Series, pli.Series], pli.Series],
exprs: Sequence[pli.Expr | str] | pli.Expr,
) -> pli.Expr:
"""
Expand All @@ -1682,7 +1695,7 @@ def cumreduce(
Parameters
----------
f
function
Function to apply over the accumulator and the value.
Fn(acc, value) -> new_value
exprs
Expand All @@ -1694,7 +1707,7 @@ def cumreduce(
exprs = [exprs]

exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(pycumreduce(f, exprs))
return pli.wrap_expr(pycumreduce(function, exprs))


def any(name: str | Sequence[str] | Sequence[pli.Expr] | pli.Expr) -> pli.Expr:
Expand Down
8 changes: 5 additions & 3 deletions py-polars/polars/internals/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
_process_null_values,
_timedelta_to_pl_duration,
deprecate_nonkeyword_arguments,
deprecated_alias,
normalise_filepath,
redirect,
)
Expand Down Expand Up @@ -3867,10 +3868,11 @@ def melt(
self._ldf.melt(id_vars, value_vars, value_name, variable_name)
)

@deprecated_alias(f="function")
@deprecate_nonkeyword_arguments()
def map(
self,
f: Callable[[pli.DataFrame], pli.DataFrame],
function: Callable[[pli.DataFrame], pli.DataFrame],
predicate_pushdown: bool = True,
projection_pushdown: bool = True,
slice_pushdown: bool = True,
Expand All @@ -3886,7 +3888,7 @@ def map(
Parameters
----------
f
function
Lambda/ function to apply.
predicate_pushdown
Allow predicate pushdown optimization to pass this node.
Expand Down Expand Up @@ -3940,7 +3942,7 @@ def map(

return self._from_pyldf(
self._ldf.map(
f,
function,
predicate_pushdown,
projection_pushdown,
slice_pushdown,
Expand Down

0 comments on commit 34e1681

Please sign in to comment.