Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(python): reorder pl.duration arguments #11641

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions py-polars/polars/functions/as_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,40 @@ def time_(

def duration(
*,
weeks: Expr | str | int | None = None,
days: Expr | str | int | None = None,
hours: Expr | str | int | None = None,
minutes: Expr | str | int | None = None,
seconds: Expr | str | int | None = None,
nanoseconds: Expr | str | int | None = None,
microseconds: Expr | str | int | None = None,
milliseconds: Expr | str | int | None = None,
minutes: Expr | str | int | None = None,
hours: Expr | str | int | None = None,
weeks: Expr | str | int | None = None,
microseconds: Expr | str | int | None = None,
nanoseconds: Expr | str | int | None = None,
time_unit: TimeUnit = "us",
) -> Expr:
"""
Create polars `Duration` from distinct time components.

Parameters
----------
weeks
Number of weeks.
days
Number of days.
hours
Number of hours.
minutes
Number of minutes.
seconds
Number of seconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
nanoseconds
Number of nanoseconds.
time_unit : {'us', 'ms', 'ns'}
Time unit of the resulting expression.

Returns
-------
Expr
Expand Down Expand Up @@ -268,6 +289,10 @@ def duration(
└─────────────────────┴─────────────────────┴─────────────────────┘

""" # noqa: W505
if weeks is not None:
weeks = parse_as_expression(weeks)
if days is not None:
days = parse_as_expression(days)
if hours is not None:
hours = parse_as_expression(hours)
if minutes is not None:
Expand All @@ -280,21 +305,17 @@ def duration(
microseconds = parse_as_expression(microseconds)
if nanoseconds is not None:
nanoseconds = parse_as_expression(nanoseconds)
if days is not None:
days = parse_as_expression(days)
if weeks is not None:
weeks = parse_as_expression(weeks)

return wrap_expr(
plr.duration(
weeks,
days,
hours,
minutes,
seconds,
nanoseconds,
microseconds,
milliseconds,
minutes,
hours,
weeks,
microseconds,
nanoseconds,
time_unit,
)
)
Expand Down
32 changes: 16 additions & 16 deletions py-polars/src/functions/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,37 +299,37 @@ pub fn dtype_cols(dtypes: Vec<Wrap<DataType>>) -> PyResult<PyExpr> {
}

#[pyfunction]
#[pyo3(signature = (days, seconds, nanoseconds, microseconds, milliseconds, minutes, hours, weeks, time_unit))]
#[pyo3(signature = (weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, time_unit))]
pub fn duration(
weeks: Option<PyExpr>,
days: Option<PyExpr>,
hours: Option<PyExpr>,
minutes: Option<PyExpr>,
seconds: Option<PyExpr>,
nanoseconds: Option<PyExpr>,
microseconds: Option<PyExpr>,
milliseconds: Option<PyExpr>,
minutes: Option<PyExpr>,
hours: Option<PyExpr>,
weeks: Option<PyExpr>,
microseconds: Option<PyExpr>,
nanoseconds: Option<PyExpr>,
time_unit: Wrap<TimeUnit>,
) -> PyExpr {
set_unwrapped_or_0!(
weeks,
days,
hours,
minutes,
seconds,
nanoseconds,
microseconds,
milliseconds,
minutes,
hours,
weeks,
microseconds,
nanoseconds,
);
let args = DurationArgs {
weeks,
days,
hours,
minutes,
seconds,
nanoseconds,
microseconds,
milliseconds,
minutes,
hours,
weeks,
microseconds,
nanoseconds,
time_unit: time_unit.0,
};
dsl::duration(args).into()
Expand Down