Skip to content

Commit

Permalink
add dt.epoch_* to series
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 13, 2021
1 parent c4d5385 commit 9187061
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions py-polars/docs/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ The following methods are available under the `Series.dt` attribute.
DateTimeNameSpace.mean
DateTimeNameSpace.round
DateTimeNameSpace.buckets
DateTimeNameSpace.epoch_days
DateTimeNameSpace.epoch_milliseconds
DateTimeNameSpace.epoch_seconds


Strings
Expand Down
33 changes: 33 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,39 @@ def round(self, rule: str, n: int) -> Series:
"""
return wrap_s(self._s.round_datetime(rule, n))

def epoch_days(self) -> Series:
"""
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_s(self._s).cast(Date).cast(Int32)

def epoch_milliseconds(self) -> Series:
"""
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) -> Series:
"""
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_s(self._s.dt_epoch_seconds())


def _to_python_datetime(
value: Union[int, float], dtype: Type[DataType]
Expand Down
4 changes: 4 additions & 0 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,10 @@ impl PySeries {
.into()),
}
}
pub fn dt_epoch_seconds(&self) -> PyResult<Self> {
let ms = self.series.timestamp().map_err(PyPolarsEr::from)?;
Ok((ms / 1000).into_series().into())
}

pub fn peak_max(&self) -> Self {
self.series.peak_max().into_series().into()
Expand Down

0 comments on commit 9187061

Please sign in to comment.