Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depr: Deprecate dt.with_time_unit in favor of cast(pl.Int64).cast(pl.Datetime(time_unit, time_zone)) #13667

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions py-polars/polars/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,17 +1327,24 @@ def timestamp(self, time_unit: TimeUnit = "us") -> Expr:
"""
return wrap_expr(self._pyexpr.dt_timestamp(time_unit))

@deprecate_function(
"Instead, first cast to `Int64` and then cast to the desired data type.",
version="0.20.5",
)
def with_time_unit(self, time_unit: TimeUnit) -> Expr:
"""
Set time unit of an expression of dtype Datetime or Duration.

.. deprecated:: 0.20.5
First cast to `Int64` and then cast to the desired data type.

This does not modify underlying data, and should be used to fix an incorrect
time unit.

Parameters
----------
time_unit : {'ns', 'us', 'ms'}
Unit of time for the `Datetime` expression.
Unit of time for the `Datetime` or `Duration` expression.

Examples
--------
Expand All @@ -1354,11 +1361,9 @@ def with_time_unit(self, time_unit: TimeUnit) -> Expr:
... }
... )
>>> df.select(
... [
... pl.col("date"),
... pl.col("date").dt.with_time_unit("us").alias("time_unit_us"),
... ]
... )
... pl.col("date"),
... pl.col("date").dt.with_time_unit("us").alias("time_unit_us"),
... ) # doctest: +SKIP
shape: (3, 2)
┌─────────────────────┬───────────────────────┐
│ date ┆ time_unit_us │
Expand Down
7 changes: 5 additions & 2 deletions py-polars/polars/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,16 @@ def with_time_unit(self, time_unit: TimeUnit) -> Series:
"""
Set time unit a Series of dtype Datetime or Duration.

.. deprecated:: 0.20.5
First cast to `Int64` and then cast to the desired data type.

This does not modify underlying data, and should be used to fix an incorrect
time unit.

Parameters
----------
time_unit : {'ns', 'us', 'ms'}
Unit of time for the `Datetime` Series.
Unit of time for the `Datetime` or `Duration` Series.

Examples
--------
Expand All @@ -911,7 +914,7 @@ def with_time_unit(self, time_unit: TimeUnit) -> Series:
... [datetime(2001, 1, 1), datetime(2001, 1, 2), datetime(2001, 1, 3)],
... dtype=pl.Datetime(time_unit="ns"),
... )
>>> s.dt.with_time_unit("us")
>>> s.dt.with_time_unit("us") # doctest: +SKIP
shape: (3,)
Series: 'datetime' [datetime[μs]]
[
Expand Down
11 changes: 6 additions & 5 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,12 @@ def test_to_list() -> None:

def test_rows() -> None:
s0 = pl.Series("date", [123543, 283478, 1243]).cast(pl.Date)
s1 = (
pl.Series("datetime", [a * 1_000_000 for a in [123543, 283478, 1243]])
.cast(pl.Datetime)
.dt.with_time_unit("ns")
)
with pytest.deprecated_call(match="`with_time_unit` is deprecated"):
s1 = (
pl.Series("datetime", [a * 1_000_000 for a in [123543, 283478, 1243]])
.cast(pl.Datetime)
.dt.with_time_unit("ns")
)
df = pl.DataFrame([s0, s1])

rows = df.rows()
Expand Down