Skip to content

Commit

Permalink
DEPR: Passing in a string column label for DataFrame.ewm(times=...) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored Sep 5, 2021
1 parent 5c81ac4 commit 31759fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ Other Deprecations
- Deprecated dropping of nuisance columns in :class:`Rolling`, :class:`Expanding`, and :class:`EWM` aggregations (:issue:`42738`)
- Deprecated :meth:`Index.reindex` with a non-unique index (:issue:`42568`)
- Deprecated :meth:`.Styler.render` in favour of :meth:`.Styler.to_html` (:issue:`42140`)
- Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)

.. ---------------------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from pandas.compat.numpy import function as nv
from pandas.util._decorators import doc
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import is_datetime64_ns_dtype
from pandas.core.dtypes.missing import isna
Expand Down Expand Up @@ -315,6 +316,15 @@ def __init__(
if not self.adjust:
raise NotImplementedError("times is not supported with adjust=False.")
if isinstance(self.times, str):
warnings.warn(
(
"Specifying times as a string column label is deprecated "
"and will be removed in a future version. Pass the column "
"into times instead."
),
FutureWarning,
stacklevel=find_stack_level(),
)
self.times = self._selected_obj[self.times]
if not is_datetime64_ns_dtype(self.times):
raise ValueError("times must be datetime64[ns] dtype.")
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/window/test_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def test_ewma_halflife_without_times(halflife_with_times):
np.arange(10).astype("datetime64[D]").astype("datetime64[ns]"),
date_range("2000", freq="D", periods=10),
date_range("2000", freq="D", periods=10).tz_localize("UTC"),
"time_col",
],
)
@pytest.mark.parametrize("min_periods", [0, 2])
Expand Down Expand Up @@ -231,3 +230,14 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype):
result = getattr(e, func)()

tm.assert_frame_equal(result, expected)


def test_times_string_col_deprecated():
# GH 43265
data = np.arange(10.0)
data[::2] = np.nan
df = DataFrame({"A": data, "time_col": date_range("2000", freq="D", periods=10)})
with tm.assert_produces_warning(FutureWarning, match="Specifying times"):
result = df.ewm(halflife="1 day", min_periods=0, times="time_col").mean()
expected = df.ewm(halflife=1.0, min_periods=0).mean()
tm.assert_frame_equal(result, expected)

0 comments on commit 31759fa

Please sign in to comment.