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(python): Deprecate dt.mean/dt.median in favor of mean/median #16888

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions py-polars/polars/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ def max(self) -> dt.date | dt.datetime | dt.timedelta | None:
"""
return wrap_s(self._s).max() # type: ignore[return-value]

@deprecate_function("Use `Series.median` instead.", version="0.20.32")
stinodego marked this conversation as resolved.
Show resolved Hide resolved
def median(self) -> TemporalLiteral | None:
"""
Return median as python DateTime.

.. deprecated:: 0.20.32
Use `Series.median` instead.

Examples
--------
>>> from datetime import date, datetime
>>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2)])
>>> s.dt.median()
>>> s.dt.median() # doctest: +SKIP
datetime.datetime(2001, 1, 1, 12, 0)
>>> date = pl.datetime_range(
... datetime(2001, 1, 1), datetime(2001, 1, 3), "1d", eager=True
Expand All @@ -175,25 +179,29 @@ def median(self) -> TemporalLiteral | None:
2001-01-02 00:00:00
2001-01-03 00:00:00
]
>>> date.dt.median()
>>> date.dt.median() # doctest: +SKIP
datetime.datetime(2001, 1, 2, 0, 0)
"""
return self._s.median()

@deprecate_function("Use `Series.mean` instead.", version="0.20.32")
def mean(self) -> TemporalLiteral | None:
"""
Return mean as python DateTime.

.. deprecated:: 0.20.32
Use `Series.mean` instead.

Examples
--------
>>> from datetime import date, datetime
>>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2)])
>>> s.dt.mean()
>>> s.dt.mean() # doctest: +SKIP
datetime.datetime(2001, 1, 1, 12, 0)
>>> s = pl.Series(
... [datetime(2001, 1, 1), datetime(2001, 1, 2), datetime(2001, 1, 3)]
... )
>>> s.dt.mean()
>>> s.dt.mean() # doctest: +SKIP
datetime.datetime(2001, 1, 2, 0, 0)
"""
return self._s.mean()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,11 @@ def test_median(
values: list[TemporalLiteral | None], expected_median: TemporalLiteral | None
) -> None:
s = pl.Series(values)
assert s.dt.median() == expected_median

if s.dtype in (pl.Datetime, pl.Duration, pl.Time):
stinodego marked this conversation as resolved.
Show resolved Hide resolved
assert s.median() == expected_median
assert s.median() == expected_median

with pytest.deprecated_call():
assert s.dt.median() == expected_median


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1100,10 +1101,11 @@ def test_mean(
values: list[TemporalLiteral | None], expected_mean: TemporalLiteral | None
) -> None:
s = pl.Series(values)
assert s.dt.mean() == expected_mean

if s.dtype in (pl.Datetime, pl.Duration, pl.Time):
stinodego marked this conversation as resolved.
Show resolved Hide resolved
assert s.mean() == expected_mean
assert s.mean() == expected_mean

with pytest.deprecated_call():
stinodego marked this conversation as resolved.
Show resolved Hide resolved
assert s.dt.mean() == expected_mean


@pytest.mark.parametrize(
Expand All @@ -1122,7 +1124,10 @@ def test_datetime_mean_with_tu(
values: list[datetime], expected_mean: datetime, time_unit: TimeUnit
) -> None:
assert pl.Series(values, dtype=pl.Duration(time_unit)).mean() == expected_mean
assert pl.Series(values, dtype=pl.Duration(time_unit)).dt.mean() == expected_mean
with pytest.deprecated_call():
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.mean() == expected_mean
)


@pytest.mark.parametrize(
Expand All @@ -1141,9 +1146,11 @@ def test_datetime_median_with_tu(
values: list[datetime], expected_median: datetime, time_unit: TimeUnit
) -> None:
assert pl.Series(values, dtype=pl.Duration(time_unit)).median() == expected_median
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.median() == expected_median
)
with pytest.deprecated_call():
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.median()
== expected_median
)


@pytest.mark.parametrize(
Expand All @@ -1162,7 +1169,10 @@ def test_duration_mean_with_tu(
values: list[timedelta], expected_mean: timedelta, time_unit: TimeUnit
) -> None:
assert pl.Series(values, dtype=pl.Duration(time_unit)).mean() == expected_mean
assert pl.Series(values, dtype=pl.Duration(time_unit)).dt.mean() == expected_mean
with pytest.deprecated_call():
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.mean() == expected_mean
)


@pytest.mark.parametrize(
Expand All @@ -1181,9 +1191,11 @@ def test_duration_median_with_tu(
values: list[timedelta], expected_median: timedelta, time_unit: TimeUnit
) -> None:
assert pl.Series(values, dtype=pl.Duration(time_unit)).median() == expected_median
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.median() == expected_median
)
with pytest.deprecated_call():
assert (
pl.Series(values, dtype=pl.Duration(time_unit)).dt.median()
== expected_median
)


def test_agg_mean_expr() -> None:
Expand Down