Skip to content

Commit

Permalink
fix(python): Fix from_epoch function signature (#6024)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jan 4, 2023
1 parent f1e5f6a commit ddd4faa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 60 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/expressions/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ These functions are available from the polars module root and can be used as exp
first
fold
format
from_epoch
groups
head
list
Expand Down
77 changes: 17 additions & 60 deletions py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2498,60 +2498,19 @@ def coalesce(


@overload
def from_epoch(
column: str | pli.Expr | pli.Series,
unit: EpochTimeUnit = ...,
*,
eager: Literal[False],
) -> pli.Expr:
...


@overload
def from_epoch(
column: str | pli.Expr | pli.Series | Sequence[int],
unit: EpochTimeUnit = ...,
*,
eager: Literal[True],
) -> pli.Series:
def from_epoch(column: str | pli.Expr, unit: EpochTimeUnit = ...) -> pli.Expr:
...


@overload
def from_epoch(
column: pli.Series | Sequence[int],
unit: EpochTimeUnit = ...,
*,
eager: Literal[True] = ...,
column: pli.Series | Sequence[int], unit: EpochTimeUnit = ...
) -> pli.Series:
...


@overload
def from_epoch(
column: str | pli.Expr,
unit: EpochTimeUnit = ...,
*,
eager: Literal[False] = ...,
) -> pli.Expr:
...


@overload
def from_epoch(
column: str | pli.Expr | pli.Series | Sequence[int],
unit: EpochTimeUnit = ...,
*,
eager: bool = ...,
) -> pli.Expr | pli.Series:
...


def from_epoch(
column: str | pli.Expr | pli.Series | Sequence[int],
unit: EpochTimeUnit = "s",
*,
eager: bool = False,
column: str | pli.Expr | pli.Series | Sequence[int], unit: EpochTimeUnit = "s"
) -> pli.Expr | pli.Series:
"""
Utility function that parses an epoch timestamp (or Unix time) to Polars Date(time).
Expand All @@ -2569,8 +2528,6 @@ def from_epoch(
Series or expression to parse integers to pl.Datetime.
unit
The unit of the timesteps since epoch time.
eager
If eager evaluation is `True`, a Series is returned instead of an Expr.
Examples
--------
Expand All @@ -2586,30 +2543,30 @@ def from_epoch(
│ 2022-10-25 07:31:39 │
└─────────────────────┘
The function can also be used in an eager context by passing a Series.
>>> s = pl.Series([12345, 12346])
>>> pl.from_epoch(s, unit="d")
shape: (2,)
Series: '' [date]
[
2003-10-20
2003-10-21
]
"""
if isinstance(column, str):
column = col(column)
elif not isinstance(column, (pli.Series, pli.Expr)):
column = pli.Series(column) # Sequence input handled by Series constructor

if unit == "d":
expr = column.cast(Date)
return column.cast(Date)
elif unit == "s":
expr = (column.cast(Int64) * 1_000_000).cast(Datetime("us"))
return (column.cast(Int64) * 1_000_000).cast(Datetime("us"))
elif unit in DTYPE_TEMPORAL_UNITS:
expr = column.cast(Datetime(unit))
return column.cast(Datetime(unit))
else:
raise ValueError(
f"'unit' must be one of {{'ns', 'us', 'ms', 's', 'd'}}, got '{unit}'."
)

if eager:
if not isinstance(column, pli.Series):
raise ValueError(
"expected 'Series or Sequence' in 'from_epoch' if 'eager=True', got"
f" {type(column)}"
)
else:
return column.to_frame().select(expr).to_series()
else:
return expr

0 comments on commit ddd4faa

Please sign in to comment.