Skip to content

Commit

Permalink
Python expr dt namespace:
Browse files Browse the repository at this point in the history
Add epoch_days, epoch_seconds and epoch_milliseconds
  • Loading branch information
ritchie46 committed Dec 11, 2021
1 parent 1fd592a commit b0d7d33
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions py-polars/docs/source/reference/expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ The following methods are available under the `expr.dt` attribute.
ExprDateTimeNameSpace.to_python_datetime
ExprDateTimeNameSpace.timestamp
ExprDateTimeNameSpace.buckets
ExprDateTimeNameSpace.epoch_days
ExprDateTimeNameSpace.epoch_milliseconds
ExprDateTimeNameSpace.epoch_seconds

Strings
-------
Expand Down
36 changes: 35 additions & 1 deletion py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Date,
Datetime,
Float64,
Int32,
Object,
UInt32,
py_type_to_dtype,
Expand Down Expand Up @@ -2644,8 +2645,41 @@ def to_python_datetime(self) -> Expr:
lambda s: s.dt.to_python_datetime(), return_dtype=Object
)

def epoch_days(self) -> Expr:
"""
Get the number of days since the unix EPOCH.
If the date is before the unix EPOCH, the number of days will be negative.
Returns
-------
Days as Int32
"""
return wrap_expr(self._pyexpr).cast(Date).cast(Int32)

def epoch_milliseconds(self) -> Expr:
"""
Get the number of milliseconds since the unix EPOCH
If the date is before the unix EPOCH, the number of milliseconds will be negative.
Returns
-------
Milliseconds as Int64
"""
return self.timestamp()

def epoch_seconds(self) -> Expr:
"""
Get the number of seconds since the unix EPOCH
If the date is before the unix EPOCH, the number of seconds will be negative.
Returns
-------
Milliseconds as Int64
"""
return wrap_expr(self._pyexpr.dt_epoch_seconds())

def timestamp(self) -> Expr:
"""Return timestamp in ms as Int64 type."""
"""Return timestamp in milliseconds as Int64 type."""
return wrap_expr(self._pyexpr.timestamp())


Expand Down
9 changes: 9 additions & 0 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@ impl PyExpr {
)
.into()
}
pub fn dt_epoch_seconds(&self) -> PyExpr {
self.clone()
.inner
.map(
|s| s.timestamp().map(|ca| (ca / 1000).into_series()),
GetOutput::from_type(DataType::Int64),
)
.into()
}

pub fn rolling_apply(&self, py: Python, window_size: usize, lambda: PyObject) -> PyExpr {
// get the pypolars module
Expand Down

0 comments on commit b0d7d33

Please sign in to comment.