Skip to content

Commit

Permalink
rust: move week/day/hour etc to 'dt()' namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 18, 2022
1 parent 641acd3 commit f40cef0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 92 deletions.
73 changes: 73 additions & 0 deletions polars/polars-lazy/src/dsl/dt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,77 @@ impl DateLikeNameSpace {
GetOutput::same_type(),
)
}

/// Get the year of a Date/Datetime
pub fn year(self) -> Expr {
let function = move |s: Series| s.year().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("year")
}

/// Get the month of a Date/Datetime
pub fn month(self) -> Expr {
let function = move |s: Series| s.month().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("month")
}
/// Extract the week from the underlying Date representation.
/// Can be performed on Date and Datetime

/// Returns the ISO week number starting from 1.
/// The return value ranges from 1 to 53. (The last week of year differs by years.)
pub fn week(self) -> Expr {
let function = move |s: Series| s.week().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("week")
}

/// Extract the week day from the underlying Date representation.
/// Can be performed on Date and Datetime.

/// Returns the weekday number where monday = 0 and sunday = 6
pub fn weekday(self) -> Expr {
let function = move |s: Series| s.weekday().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("weekday")
}

/// Get the month of a Date/Datetime
pub fn day(self) -> Expr {
let function = move |s: Series| s.day().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("day")
}
/// Get the ordinal_day of a Date/Datetime
pub fn ordinal_day(self) -> Expr {
let function = move |s: Series| s.ordinal_day().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("ordinal_day")
}
/// Get the hour of a Datetime/Time64
pub fn hour(self) -> Expr {
let function = move |s: Series| s.hour().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("hour")
}
/// Get the minute of a Datetime/Time64
pub fn minute(self) -> Expr {
let function = move |s: Series| s.minute().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("minute")
}

/// Get the second of a Datetime/Time64
pub fn second(self) -> Expr {
let function = move |s: Series| s.second().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("second")
}
/// Get the nanosecond of a Time64
pub fn nanosecond(self) -> Expr {
let function = move |s: Series| s.nanosecond().map(|ca| ca.into_series());
self.0.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("nanosecond")
}

}
82 changes: 0 additions & 82 deletions polars/polars-lazy/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,88 +1421,6 @@ impl Expr {
.with_fmt("is_in")
}

/// Get the year of a Date/Datetime
#[cfg(feature = "temporal")]
pub fn year(self) -> Expr {
let function = move |s: Series| s.year().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("year")
}

/// Get the month of a Date/Datetime
#[cfg(feature = "temporal")]
pub fn month(self) -> Expr {
let function = move |s: Series| s.month().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("month")
}
/// Extract the week from the underlying Date representation.
/// Can be performed on Date and Datetime

/// Returns the ISO week number starting from 1.
/// The return value ranges from 1 to 53. (The last week of year differs by years.)
#[cfg(feature = "temporal")]
pub fn week(self) -> Expr {
let function = move |s: Series| s.week().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("week")
}

/// Extract the week day from the underlying Date representation.
/// Can be performed on Date and Datetime.

/// Returns the weekday number where monday = 0 and sunday = 6
#[cfg(feature = "temporal")]
pub fn weekday(self) -> Expr {
let function = move |s: Series| s.weekday().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("weekday")
}

/// Get the month of a Date/Datetime
#[cfg(feature = "temporal")]
pub fn day(self) -> Expr {
let function = move |s: Series| s.day().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("day")
}
/// Get the ordinal_day of a Date/Datetime
#[cfg(feature = "temporal")]
pub fn ordinal_day(self) -> Expr {
let function = move |s: Series| s.ordinal_day().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("ordinal_day")
}
/// Get the hour of a Datetime/Time64
#[cfg(feature = "temporal")]
pub fn hour(self) -> Expr {
let function = move |s: Series| s.hour().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("hour")
}
/// Get the minute of a Datetime/Time64
#[cfg(feature = "temporal")]
pub fn minute(self) -> Expr {
let function = move |s: Series| s.minute().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("minute")
}

/// Get the second of a Datetime/Time64
#[cfg(feature = "temporal")]
pub fn second(self) -> Expr {
let function = move |s: Series| s.second().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("second")
}
/// Get the nanosecond of a Time64
#[cfg(feature = "temporal")]
pub fn nanosecond(self) -> Expr {
let function = move |s: Series| s.nanosecond().map(|ca| ca.into_series());
self.map(function, GetOutput::from_type(DataType::UInt32))
.with_fmt("nanosecond")
}

/// Sort this column by the ordering of another column.
/// Can also be used in a groupby context to sort the groups.
pub fn sort_by<E: AsRef<[Expr]>, R: AsRef<[bool]>>(self, by: E, reverse: R) -> Expr {
Expand Down
20 changes: 10 additions & 10 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,34 +594,34 @@ impl PyExpr {
}

pub fn year(&self) -> PyExpr {
self.clone().inner.year().into()
self.clone().inner.dt().year().into()
}
pub fn month(&self) -> PyExpr {
self.clone().inner.month().into()
self.clone().inner.dt().month().into()
}
pub fn week(&self) -> PyExpr {
self.clone().inner.week().into()
self.clone().inner.dt().week().into()
}
pub fn weekday(&self) -> PyExpr {
self.clone().inner.weekday().into()
self.clone().inner.dt().weekday().into()
}
pub fn day(&self) -> PyExpr {
self.clone().inner.day().into()
self.clone().inner.dt().day().into()
}
pub fn ordinal_day(&self) -> PyExpr {
self.clone().inner.ordinal_day().into()
self.clone().inner.dt().ordinal_day().into()
}
pub fn hour(&self) -> PyExpr {
self.clone().inner.hour().into()
self.clone().inner.dt().hour().into()
}
pub fn minute(&self) -> PyExpr {
self.clone().inner.minute().into()
self.clone().inner.dt().minute().into()
}
pub fn second(&self) -> PyExpr {
self.clone().inner.second().into()
self.clone().inner.dt().second().into()
}
pub fn nanosecond(&self) -> PyExpr {
self.clone().inner.nanosecond().into()
self.clone().inner.dt().nanosecond().into()
}
pub fn duration_days(&self) -> PyExpr {
self.inner
Expand Down

0 comments on commit f40cef0

Please sign in to comment.