Skip to content

Commit

Permalink
switch to using pytest.deprecated_call
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorWinstral committed Apr 8, 2024
1 parent 621cc48 commit e2c0a4b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 73 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5937,7 +5937,7 @@ def upsample(
.. deprecated:: 0.20.19
This argument is deprecated and will be removed in the next breaking
release. Instead, chain `upsample` with `.dt.offset_by`.
release. Instead, chain `upsample` with `dt.offset_by`.
group_by
First group by these columns and then upsample for every group.
maintain_order
Expand Down Expand Up @@ -5988,7 +5988,7 @@ def upsample(
if offset is not None:
issue_deprecation_warning(
"`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `upsample` with `.dt.offset_by`.",
"Instead, chain `upsample` with `dt.offset_by`.",
version="0.20.19",
)
offset = deprecate_saturating(offset)
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def truncate(
if offset is not None:
issue_deprecation_warning(
"`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.truncate` with `.dt.offset_by`.",
"Instead, chain `dt.truncate` with `dt.offset_by`.",
version="0.20.19",
)
if not isinstance(every, pl.Expr):
Expand Down Expand Up @@ -250,7 +250,7 @@ def round(
.. deprecated:: 0.20.19
This argument is deprecated and will be removed in the next breaking
release. Instead, chain `dt.round` with `.dt.offset_by`.
release. Instead, chain `dt.round` with `dt.offset_by`.
ambiguous
Determine how to deal with ambiguous datetimes:
Expand Down Expand Up @@ -346,7 +346,7 @@ def round(
if offset is not None:
issue_deprecation_warning(
"`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.round` with `.dt.offset_by`.",
"Instead, chain `dt.round` with `dt.offset_by`.",
version="0.20.19",
)
if offset is None:
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ def truncate(
.. deprecated:: 0.20.19
This argument is deprecated and will be removed in the next breaking
release. Instead, chain `dt.truncate` with `.dt.offset_by`.
release. Instead, chain `dt.truncate` with `dt.offset_by`.
use_earliest
Determine how to deal with ambiguous datetimes:
Expand Down Expand Up @@ -1743,7 +1743,7 @@ def round(
.. deprecated:: 0.20.19
This argument is deprecated and will be removed in the next breaking
release. Instead, chain `dt.round` with `.dt.offset_by`.
release. Instead, chain `dt.round` with `dt.offset_by`.
ambiguous
Determine how to deal with ambiguous datetimes:
Expand Down
101 changes: 35 additions & 66 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,25 +579,26 @@ def test_upsample(
}
).with_columns(pl.col("time").dt.replace_time_zone(time_zone).set_sorted())

context_manager: contextlib.AbstractContextManager[pytest.WarningsRecorder | None]
msg = (
"`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `upsample` with `dt.offset_by`."
)
if offset is not None:
with pytest.warns(
context_manager = pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `upsample` with `.dt.offset_by`.",
):
up = df.upsample(
time_column="time",
every="1mo",
group_by="admin",
maintain_order=True,
offset=offset,
).select(pl.all().forward_fill())
match=msg,
)
else:
context_manager = contextlib.nullcontext()

with context_manager:
up = df.upsample(
time_column="time",
every="1mo",
group_by="admin",
maintain_order=True,
offset=offset,
).select(pl.all().forward_fill())
# this print will panic if timezones feature is not activated
# don't remove
Expand Down Expand Up @@ -661,36 +662,20 @@ def test_offset_deprecated() -> None:
).sort("time")

# truncate
with pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.truncate` with `.dt.offset_by`.",
):
with pytest.deprecated_call():
df.select(pl.col("time").dt.truncate(every="1mo", offset="1d"))

# round
with pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.round` with `.dt.offset_by`.",
):
with pytest.deprecated_call():
df.select(pl.col("time").dt.round(every="1mo", offset="1d"))

ser = df.to_series(0)
# truncate
with pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.truncate` with `.dt.offset_by`.",
):
with pytest.deprecated_call():
ser.dt.truncate(every="1mo", offset="1d")

# round
with pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `dt.round` with `.dt.offset_by`.",
):
with pytest.deprecated_call():
ser.dt.round(every="1mo", offset="1d")


Expand Down Expand Up @@ -723,14 +708,20 @@ def test_upsample_crossing_dst(
"values": [1, 2, 3],
}
)
context_manager: contextlib.AbstractContextManager[pytest.WarningsRecorder | None]
msg = (
"`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `upsample` with `dt.offset_by`."
)
if offset is not None:
with pytest.warns(
context_manager = pytest.warns(
DeprecationWarning,
match="`offset` is deprecated and will be removed in the next breaking release. "
"Instead, chain `upsample` with `.dt.offset_by`.",
):
result = df.upsample(time_column="time", every="1d", offset=offset)
match=msg,
)
else:
context_manager = contextlib.nullcontext()

with context_manager:
result = df.upsample(time_column="time", every="1d", offset=offset)
expected = pl.DataFrame(
{
Expand Down Expand Up @@ -1843,21 +1834,15 @@ def test_replace_time_zone_ambiguous_null() -> None:

def test_use_earliest_deprecation() -> None:
# strptime
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=True` with `ambiguous='earliest'`",
):
with pytest.deprecated_call():
result = pl.Series(["2020-10-25 01:00"]).str.strptime(
pl.Datetime("us", "Europe/London"), use_earliest=True
)
expected = pl.Series(["2020-10-25 01:00"]).str.strptime(
pl.Datetime("us", "Europe/London"), ambiguous="earliest"
)
assert_series_equal(result, expected)
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=False` with `ambiguous='latest'`",
):
with pytest.deprecated_call():
result = pl.Series(["2020-10-25 01:00"]).str.strptime(
pl.Datetime("us", "Europe/London"), use_earliest=False
)
Expand All @@ -1870,48 +1855,32 @@ def test_use_earliest_deprecation() -> None:
ser = pl.Series(["2020-10-25 01:00"]).str.to_datetime(
time_zone="Europe/London", ambiguous="latest"
)
with pytest.warns(
DeprecationWarning,
):
with pytest.deprecated_call():
result = ser.dt.truncate("1h", use_earliest=True)
expected = ser.dt.truncate("1h")
assert_series_equal(result, expected)
with pytest.warns(
DeprecationWarning,
):
with pytest.deprecated_call():
result = ser.dt.truncate("1h", use_earliest=True)
expected = ser.dt.truncate("1h")
assert_series_equal(result, expected)

# replace_time_zone
ser = pl.Series([datetime(2020, 10, 25, 1)])
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=True` with `ambiguous='earliest'`",
):
with pytest.deprecated_call():
result = ser.dt.replace_time_zone("Europe/London", use_earliest=True)
expected = ser.dt.replace_time_zone("Europe/London", ambiguous="earliest")
assert_series_equal(result, expected)
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=False` with `ambiguous='latest'`",
):
with pytest.deprecated_call():
result = ser.dt.replace_time_zone("Europe/London", use_earliest=False)
expected = ser.dt.replace_time_zone("Europe/London", ambiguous="latest")
assert_series_equal(result, expected)

# pl.datetime
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=True` with `ambiguous='earliest'`",
):
with pytest.deprecated_call():
result = pl.select(pl.datetime(2020, 10, 25, 1, use_earliest=True))["datetime"]
expected = pl.select(pl.datetime(2020, 10, 25, 1, ambiguous="earliest"))["datetime"]
assert_series_equal(result, expected)
with pytest.warns(
DeprecationWarning,
match="Please replace `use_earliest=False` with `ambiguous='latest'`",
):
with pytest.deprecated_call():
result = pl.select(pl.datetime(2020, 10, 25, 1, use_earliest=False))["datetime"]
expected = pl.select(pl.datetime(2020, 10, 25, 1, ambiguous="latest"))["datetime"]
assert_series_equal(result, expected)
Expand Down

0 comments on commit e2c0a4b

Please sign in to comment.