Skip to content

Commit

Permalink
Rename periods to n
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Oct 21, 2023
1 parent 04357ef commit 564ba1c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 50 deletions.
26 changes: 13 additions & 13 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7456,13 +7456,14 @@ def partition_by(

return partitions

def shift(self, periods: int) -> Self:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int = 1) -> Self:
"""
Shift values by the given period.
Shift values by the given number of places.
Parameters
----------
periods
n
Number of places to shift (may be negative).
See Also
Expand All @@ -7478,7 +7479,7 @@ def shift(self, periods: int) -> Self:
... "ham": ["a", "b", "c"],
... }
... )
>>> df.shift(periods=1)
>>> df.shift(1)
shape: (3, 3)
┌──────┬──────┬──────┐
│ foo ┆ bar ┆ ham │
Expand All @@ -7489,7 +7490,7 @@ def shift(self, periods: int) -> Self:
│ 1 ┆ 6 ┆ a │
│ 2 ┆ 7 ┆ b │
└──────┴──────┴──────┘
>>> df.shift(periods=-1)
>>> df.shift(-1)
shape: (3, 3)
┌──────┬──────┬──────┐
│ foo ┆ bar ┆ ham │
Expand All @@ -7502,22 +7503,23 @@ def shift(self, periods: int) -> Self:
└──────┴──────┴──────┘
"""
return self._from_pydf(self._df.shift(periods))
return self._from_pydf(self._df.shift(n))

@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift_and_fill(
self,
fill_value: int | str | float,
*,
periods: int = 1,
n: int = 1,
) -> DataFrame:
"""
Shift the values by a given period and fill the resulting null values.
Shift values by the given number of places and fill the resulting null values.
Parameters
----------
fill_value
fill None values with this value.
periods
n
Number of places to shift (may be negative).
Examples
Expand All @@ -7529,7 +7531,7 @@ def shift_and_fill(
... "ham": ["a", "b", "c"],
... }
... )
>>> df.shift_and_fill(periods=1, fill_value=0)
>>> df.shift_and_fill(n=1, fill_value=0)
shape: (3, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
Expand All @@ -7543,9 +7545,7 @@ def shift_and_fill(
"""
return (
self.lazy()
.shift_and_fill(fill_value=fill_value, periods=periods)
.collect(_eager=True)
self.lazy().shift_and_fill(fill_value=fill_value, n=n).collect(_eager=True)
)

def is_duplicated(self) -> Series:
Expand Down
16 changes: 9 additions & 7 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2351,13 +2351,14 @@ def take(
indices_lit = parse_as_expression(indices) # type: ignore[arg-type]
return self._from_pyexpr(self._pyexpr.take(indices_lit))

def shift(self, periods: int = 1) -> Self:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int = 1) -> Self:
"""
Shift the values by a given period.
Parameters
----------
periods
n
Number of places to shift (may be negative).
Examples
Expand All @@ -2377,13 +2378,14 @@ def shift(self, periods: int = 1) -> Self:
└─────┴─────────────┘
"""
return self._from_pyexpr(self._pyexpr.shift(periods))
return self._from_pyexpr(self._pyexpr.shift(n))

@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift_and_fill(
self,
fill_value: IntoExpr,
*,
periods: int = 1,
n: int = 1,
) -> Self:
"""
Shift the values by a given period and fill the resulting null values.
Expand All @@ -2392,13 +2394,13 @@ def shift_and_fill(
----------
fill_value
Fill None values with the result of this expression.
periods
n
Number of places to shift (may be negative).
Examples
--------
>>> df = pl.DataFrame({"foo": [1, 2, 3, 4]})
>>> df.with_columns(foo_shifted=pl.col("foo").shift_and_fill("a", periods=1))
>>> df.with_columns(foo_shifted=pl.col("foo").shift_and_fill("a", n=1))
shape: (4, 2)
┌─────┬─────────────┐
│ foo ┆ foo_shifted │
Expand All @@ -2413,7 +2415,7 @@ def shift_and_fill(
"""
fill_value = parse_as_expression(fill_value, str_as_lit=True)
return self._from_pyexpr(self._pyexpr.shift_and_fill(periods, fill_value))
return self._from_pyexpr(self._pyexpr.shift_and_fill(n, fill_value))

def fill_null(
self,
Expand Down
14 changes: 9 additions & 5 deletions py-polars/polars/expr/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from polars import functions as F
from polars.utils._parse_expr_input import parse_as_expression
from polars.utils._wrap import wrap_expr
from polars.utils.deprecation import deprecate_renamed_function
from polars.utils.deprecation import (
deprecate_renamed_function,
deprecate_renamed_parameter,
)

if TYPE_CHECKING:
from datetime import date, datetime, time
Expand Down Expand Up @@ -712,13 +715,14 @@ def diff(self, n: int = 1, null_behavior: NullBehavior = "ignore") -> Expr:
"""
return wrap_expr(self._pyexpr.list_diff(n, null_behavior))

def shift(self, periods: int | IntoExprColumn = 1) -> Expr:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int | IntoExprColumn = 1) -> Expr:
"""
Shift values by the given period.
Parameters
----------
periods
n
Number of places to shift (may be negative).
Examples
Expand All @@ -733,8 +737,8 @@ def shift(self, periods: int | IntoExprColumn = 1) -> Expr:
]
"""
periods = parse_as_expression(periods)
return wrap_expr(self._pyexpr.list_shift(periods))
n = parse_as_expression(n)
return wrap_expr(self._pyexpr.list_shift(n))

def slice(
self, offset: int | str | Expr, length: int | str | Expr | None = None
Expand Down
26 changes: 14 additions & 12 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4206,13 +4206,14 @@ def reverse(self) -> Self:
"""
return self._from_pyldf(self._ldf.reverse())

def shift(self, periods: int) -> Self:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int = 1) -> Self:
"""
Shift the values by a given period.
Shift values by the given number of places.
Parameters
----------
periods
n
Number of places to shift (may be negative).
Examples
Expand All @@ -4223,7 +4224,7 @@ def shift(self, periods: int) -> Self:
... "b": [2, 4, 6],
... }
... )
>>> lf.shift(periods=1).collect()
>>> lf.shift(1).collect()
shape: (3, 2)
┌──────┬──────┐
│ a ┆ b │
Expand All @@ -4234,7 +4235,7 @@ def shift(self, periods: int) -> Self:
│ 1 ┆ 2 │
│ 3 ┆ 4 │
└──────┴──────┘
>>> lf.shift(periods=-1).collect()
>>> lf.shift(-1).collect()
shape: (3, 2)
┌──────┬──────┐
│ a ┆ b │
Expand All @@ -4247,22 +4248,23 @@ def shift(self, periods: int) -> Self:
└──────┴──────┘
"""
return self._from_pyldf(self._ldf.shift(periods))
return self._from_pyldf(self._ldf.shift(n))

@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift_and_fill(
self,
fill_value: Expr | int | str | float,
*,
periods: int = 1,
n: int = 1,
) -> Self:
"""
Shift the values by a given period and fill the resulting null values.
Shift values by the given number of places and fill the resulting null values.
Parameters
----------
fill_value
fill None values with the result of this expression.
periods
n
Number of places to shift (may be negative).
Examples
Expand All @@ -4273,7 +4275,7 @@ def shift_and_fill(
... "b": [2, 4, 6],
... }
... )
>>> lf.shift_and_fill(fill_value=0, periods=1).collect()
>>> lf.shift_and_fill(fill_value=0, n=1).collect()
shape: (3, 2)
┌─────┬─────┐
│ a ┆ b │
Expand All @@ -4284,7 +4286,7 @@ def shift_and_fill(
│ 1 ┆ 2 │
│ 3 ┆ 4 │
└─────┴─────┘
>>> lf.shift_and_fill(periods=-1, fill_value=0).collect()
>>> lf.shift_and_fill(fill_value=0, n=-1).collect()
shape: (3, 2)
┌─────┬─────┐
│ a ┆ b │
Expand All @@ -4299,7 +4301,7 @@ def shift_and_fill(
"""
if not isinstance(fill_value, pl.Expr):
fill_value = F.lit(fill_value)
return self._from_pyldf(self._ldf.shift_and_fill(periods, fill_value._pyexpr))
return self._from_pyldf(self._ldf.shift_and_fill(n, fill_value._pyexpr))

def slice(self, offset: int, length: int | None = None) -> Self:
"""
Expand Down
10 changes: 7 additions & 3 deletions py-polars/polars/series/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from polars import functions as F
from polars.series.utils import expr_dispatch
from polars.utils._wrap import wrap_s
from polars.utils.deprecation import deprecate_renamed_function
from polars.utils.deprecation import (
deprecate_renamed_function,
deprecate_renamed_parameter,
)

if TYPE_CHECKING:
from datetime import date, datetime, time
Expand Down Expand Up @@ -385,13 +388,14 @@ def diff(self, n: int = 1, null_behavior: NullBehavior = "ignore") -> Series:
"""

def shift(self, periods: int | IntoExprColumn = 1) -> Series:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int | IntoExprColumn = 1) -> Series:
"""
Shift values by the given period.
Parameters
----------
periods
n
Number of places to shift (may be negative).
Examples
Expand Down
22 changes: 12 additions & 10 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5082,22 +5082,28 @@ def map_elements(
self._s.apply_lambda(function, pl_return_dtype, skip_nulls)
)

def shift(self, periods: int = 1) -> Series:
@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift(self, n: int = 1) -> Series:
"""
Shift the values by a given period.
Parameters
----------
n
Number of places to shift (may be negative).
Examples
--------
>>> s = pl.Series("a", [1, 2, 3])
>>> s.shift(periods=1)
>>> s.shift(1)
shape: (3,)
Series: 'a' [i64]
[
null
1
2
]
>>> s.shift(periods=-1)
>>> s.shift(-1)
shape: (3,)
Series: 'a' [i64]
[
Expand All @@ -5106,18 +5112,14 @@ def shift(self, periods: int = 1) -> Series:
null
]
Parameters
----------
periods
Number of places to shift (may be negative).
"""

@deprecate_renamed_parameter("periods", "n", version="0.19.11")
def shift_and_fill(
self,
fill_value: int | Expr,
*,
periods: int = 1,
n: int = 1,
) -> Series:
"""
Shift the values by a given period and fill the resulting null values.
Expand All @@ -5126,7 +5128,7 @@ def shift_and_fill(
----------
fill_value
Fill None values with the result of this expression.
periods
n
Number of places to shift (may be negative).
"""
Expand Down

0 comments on commit 564ba1c

Please sign in to comment.