Skip to content

Commit

Permalink
add duration minutes (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 23, 2022
1 parent 0170155 commit fbd4824
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
36 changes: 24 additions & 12 deletions polars/polars-time/src/chunkedarray/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ pub trait DurationMethods {
/// Extract the days from a `Duration`
fn days(&self) -> Int64Chunked;

/// Extract the minutes from a `Duration`
fn minutes(&self) -> Int64Chunked;

/// Extract the seconds from a `Duration`
fn seconds(&self) -> Int64Chunked;

/// Extract the milliseconds from a `Duration`
fn milliseconds(&self) -> Int64Chunked;

/// Extract the nanoseconds from a `Duration`
fn nanoseconds(&self) -> Int64Chunked;

/// Extract the seconds from a `Duration`
fn seconds(&self) -> Int64Chunked;
}

impl DurationMethods for DurationChunked {
Expand All @@ -42,6 +45,24 @@ impl DurationMethods for DurationChunked {
}
}

/// Extract the seconds from a `Duration`
fn minutes(&self) -> Int64Chunked {
match self.time_unit() {
TimeUnit::Milliseconds => &self.0 / (MILLISECONDS * 60),
TimeUnit::Microseconds => &self.0 / (MICROSECONDS * 60),
TimeUnit::Nanoseconds => &self.0 / (NANOSECONDS * 60),
}
}

/// Extract the seconds from a `Duration`
fn seconds(&self) -> Int64Chunked {
match self.time_unit() {
TimeUnit::Milliseconds => &self.0 / MILLISECONDS,
TimeUnit::Microseconds => &self.0 / MICROSECONDS,
TimeUnit::Nanoseconds => &self.0 / NANOSECONDS,
}
}

/// Extract the milliseconds from a `Duration`
fn milliseconds(&self) -> Int64Chunked {
match self.time_unit() {
Expand All @@ -59,13 +80,4 @@ impl DurationMethods for DurationChunked {
TimeUnit::Nanoseconds => self.0.clone(),
}
}

/// Extract the seconds from a `Duration`
fn seconds(&self) -> Int64Chunked {
match self.time_unit() {
TimeUnit::Milliseconds => &self.0 / MILLISECONDS,
TimeUnit::Microseconds => &self.0 / MICROSECONDS,
TimeUnit::Nanoseconds => &self.0 / NANOSECONDS,
}
}
}
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ The following methods are available under the `expr.dt` attribute.
ExprDateTimeNameSpace.cast_time_unit
ExprDateTimeNameSpace.days
ExprDateTimeNameSpace.hours
ExprDateTimeNameSpace.minutes
ExprDateTimeNameSpace.seconds
ExprDateTimeNameSpace.milliseconds
ExprDateTimeNameSpace.nanoseconds
Expand Down
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ The following methods are available under the `Series.dt` attribute.
DateTimeNameSpace.cast_time_unit
DateTimeNameSpace.days
DateTimeNameSpace.hours
DateTimeNameSpace.minutes
DateTimeNameSpace.seconds
DateTimeNameSpace.milliseconds
DateTimeNameSpace.nanoseconds
Expand Down
10 changes: 10 additions & 0 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4415,6 +4415,16 @@ def hours(self) -> Expr:
"""
return wrap_expr(self._pyexpr.duration_hours())

def minutes(self) -> Expr:
"""
Extract the minutes from a Duration type.
Returns
-------
A series of dtype Int64
"""
return wrap_expr(self._pyexpr.duration_minutes())

def seconds(self) -> Expr:
"""
Extract the seconds from a Duration type.
Expand Down
10 changes: 10 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4896,6 +4896,16 @@ def hours(self) -> Series:
"""
return pli.select(pli.lit(wrap_s(self._s)).dt.hours()).to_series()

def minutes(self) -> Series:
"""
Extract the minutes from a Duration type.
Returns
-------
A series of dtype Int64
"""
return pli.select(pli.lit(wrap_s(self._s)).dt.minutes()).to_series()

def seconds(self) -> Series:
"""
Extract the seconds from a Duration type.
Expand Down
11 changes: 11 additions & 0 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,17 @@ impl PyExpr {
)
.into()
}

pub fn duration_minutes(&self) -> PyExpr {
self.inner
.clone()
.map(
|s| Ok(s.duration()?.minutes().into_series()),
GetOutput::from_type(DataType::Int64),
)
.into()
}

pub fn duration_seconds(&self) -> PyExpr {
self.inner
.clone()
Expand Down

0 comments on commit fbd4824

Please sign in to comment.