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

depr(python,rust!): Rename dt.seconds to dt.total_seconds (likewise for days, hours, minutes, milliseconds, microseconds, and nanoseconds) #12179

Merged
merged 5 commits into from
Nov 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

out = df.group_by_dynamic("time", every="1mo", period="1mo", closed="left").agg(
pl.col("time").cumcount().reverse().head(3).alias("day/eom"),
((pl.col("time") - pl.col("time").first()).last().dt.days() + 1).alias(
((pl.col("time") - pl.col("time").first()).last().dt.total_days() + 1).alias(
"days_in_month"
),
)
Expand Down
7 changes: 7 additions & 0 deletions py-polars/docs/source/reference/expressions/temporal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ The following methods are available under the `expr.dt` attribute.
Expr.dt.time
Expr.dt.timestamp
Expr.dt.to_string
Expr.dt.total_days
Expr.dt.total_hours
Expr.dt.total_microseconds
Expr.dt.total_milliseconds
Expr.dt.total_minutes
Expr.dt.total_nanoseconds
Expr.dt.total_seconds
Expr.dt.truncate
Expr.dt.week
Expr.dt.weekday
Expand Down
7 changes: 7 additions & 0 deletions py-polars/docs/source/reference/series/temporal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ The following methods are available under the `Series.dt` attribute.
Series.dt.time
Series.dt.timestamp
Series.dt.to_string
Series.dt.total_days
Series.dt.total_hours
Series.dt.total_microseconds
Series.dt.total_milliseconds
Series.dt.total_minutes
Series.dt.total_nanoseconds
Series.dt.total_seconds
Series.dt.truncate
Series.dt.week
Series.dt.weekday
Expand Down
154 changes: 113 additions & 41 deletions py-polars/polars/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from polars.utils._parse_expr_input import parse_as_expression
from polars.utils._wrap import wrap_expr
from polars.utils.convert import _timedelta_to_pl_duration
from polars.utils.deprecation import rename_use_earliest_to_ambiguous
from polars.utils.deprecation import (
deprecate_renamed_function,
rename_use_earliest_to_ambiguous,
)

if TYPE_CHECKING:
from datetime import timedelta
Expand Down Expand Up @@ -1454,9 +1457,9 @@ def replace_time_zone(
self._pyexpr.dt_replace_time_zone(time_zone, ambiguous._pyexpr)
)

def days(self) -> Expr:
def total_days(self) -> Expr:
"""
Extract the days from a Duration type.
Extract the total days from a Duration type.

Returns
-------
Expand All @@ -1476,7 +1479,7 @@ def days(self) -> Expr:
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.days().alias("days_diff"),
... pl.col("date").diff().dt.total_days().alias("days_diff"),
... ]
... )
shape: (3, 2)
Expand All @@ -1491,11 +1494,11 @@ def days(self) -> Expr:
└─────────────────────┴───────────┘

"""
return wrap_expr(self._pyexpr.duration_days())
return wrap_expr(self._pyexpr.dt_total_days())

def hours(self) -> Expr:
def total_hours(self) -> Expr:
"""
Extract the hours from a Duration type.
Extract the total hours from a Duration type.

Returns
-------
Expand All @@ -1515,7 +1518,7 @@ def hours(self) -> Expr:
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.hours().alias("hours_diff"),
... pl.col("date").diff().dt.total_hours().alias("hours_diff"),
... ]
... )
shape: (4, 2)
Expand All @@ -1531,11 +1534,11 @@ def hours(self) -> Expr:
└─────────────────────┴────────────┘

"""
return wrap_expr(self._pyexpr.duration_hours())
return wrap_expr(self._pyexpr.dt_total_hours())

def minutes(self) -> Expr:
def total_minutes(self) -> Expr:
"""
Extract the minutes from a Duration type.
Extract the total minutes from a Duration type.

Returns
-------
Expand All @@ -1555,7 +1558,7 @@ def minutes(self) -> Expr:
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.minutes().alias("minutes_diff"),
... pl.col("date").diff().dt.total_minutes().alias("minutes_diff"),
... ]
... )
shape: (4, 2)
Expand All @@ -1571,11 +1574,11 @@ def minutes(self) -> Expr:
└─────────────────────┴──────────────┘

"""
return wrap_expr(self._pyexpr.duration_minutes())
return wrap_expr(self._pyexpr.dt_total_minutes())

def seconds(self) -> Expr:
def total_seconds(self) -> Expr:
"""
Extract the seconds from a Duration type.
Extract the total seconds from a Duration type.

Returns
-------
Expand All @@ -1596,10 +1599,8 @@ def seconds(self) -> Expr:
... }
... )
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.seconds().alias("seconds_diff"),
... ]
... pl.col("date"),
... pl.col("date").diff().dt.total_seconds().alias("seconds_diff"),
... )
shape: (5, 2)
┌─────────────────────┬──────────────┐
Expand All @@ -1615,11 +1616,11 @@ def seconds(self) -> Expr:
└─────────────────────┴──────────────┘

"""
return wrap_expr(self._pyexpr.duration_seconds())
return wrap_expr(self._pyexpr.dt_total_seconds())

def milliseconds(self) -> Expr:
def total_milliseconds(self) -> Expr:
"""
Extract the milliseconds from a Duration type.
Extract the total milliseconds from a Duration type.

Returns
-------
Expand All @@ -1640,10 +1641,8 @@ def milliseconds(self) -> Expr:
... }
... )
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.milliseconds().alias("milliseconds_diff"),
... ]
... pl.col("date"),
... milliseconds_diff=pl.col("date").diff().dt.total_milliseconds(),
... )
shape: (1_001, 2)
┌─────────────────────────┬───────────────────┐
Expand All @@ -1663,11 +1662,11 @@ def milliseconds(self) -> Expr:
└─────────────────────────┴───────────────────┘

"""
return wrap_expr(self._pyexpr.duration_milliseconds())
return wrap_expr(self._pyexpr.dt_total_milliseconds())

def microseconds(self) -> Expr:
def total_microseconds(self) -> Expr:
"""
Extract the microseconds from a Duration type.
Extract the total microseconds from a Duration type.

Returns
-------
Expand All @@ -1688,10 +1687,8 @@ def microseconds(self) -> Expr:
... }
... )
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.microseconds().alias("microseconds_diff"),
... ]
... pl.col("date"),
... microseconds_diff=pl.col("date").diff().dt.total_microseconds(),
... )
shape: (1_001, 2)
┌─────────────────────────┬───────────────────┐
Expand All @@ -1711,11 +1708,11 @@ def microseconds(self) -> Expr:
└─────────────────────────┴───────────────────┘

"""
return wrap_expr(self._pyexpr.duration_microseconds())
return wrap_expr(self._pyexpr.dt_total_microseconds())

def nanoseconds(self) -> Expr:
def total_nanoseconds(self) -> Expr:
"""
Extract the nanoseconds from a Duration type.
Extract the total nanoseconds from a Duration type.

Returns
-------
Expand All @@ -1736,10 +1733,8 @@ def nanoseconds(self) -> Expr:
... }
... )
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").diff().dt.nanoseconds().alias("nanoseconds_diff"),
... ]
... pl.col("date"),
... nanoseconds_diff=pl.col("date").diff().dt.total_nanoseconds(),
... )
shape: (1_001, 2)
┌─────────────────────────┬──────────────────┐
Expand All @@ -1759,7 +1754,7 @@ def nanoseconds(self) -> Expr:
└─────────────────────────┴──────────────────┘

"""
return wrap_expr(self._pyexpr.duration_nanoseconds())
return wrap_expr(self._pyexpr.dt_total_nanoseconds())

def offset_by(self, by: str | Expr) -> Expr:
"""
Expand Down Expand Up @@ -2018,3 +2013,80 @@ def dst_offset(self) -> Expr:
└─────────────────────────────┴──────────────┘
"""
return wrap_expr(self._pyexpr.dt_dst_offset())

@deprecate_renamed_function("total_days", version="0.19.13")
def days(self) -> Expr:
"""
Extract the total days from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_days` instead.

"""
return self.total_days()

@deprecate_renamed_function("total_hours", version="0.19.13")
def hours(self) -> Expr:
"""
Extract the total hours from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_hours` instead.

"""
return self.total_hours()

@deprecate_renamed_function("total_minutes", version="0.19.13")
def minutes(self) -> Expr:
"""
Extract the total minutes from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_minutes` instead.

"""
return self.total_minutes()

@deprecate_renamed_function("total_seconds", version="0.19.13")
def seconds(self) -> Expr:
"""
Extract the total seconds from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_seconds` instead.

"""
return self.total_seconds()

@deprecate_renamed_function("total_milliseconds", version="0.19.13")
def milliseconds(self) -> Expr:
"""
Extract the total milliseconds from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_milliseconds` instead.

"""
return self.total_milliseconds()

@deprecate_renamed_function("total_microseconds", version="0.19.13")
def microseconds(self) -> Expr:
"""
Extract the total microseconds from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_microseconds` instead.

"""
return self.total_microseconds()

@deprecate_renamed_function("total_nanoseconds", version="0.19.13")
def nanoseconds(self) -> Expr:
"""
Extract the total nanoseconds from a Duration type.

.. deprecated:: 0.19.13
Use :meth:`total_nanoseconds` instead.

"""
return self.total_nanoseconds()
Loading