Skip to content

Commit 4b365f6

Browse files
committed
Deprecate center on df.expanding
`df.expanding(center=True)` currently returns non-sensical results and it is unclear what results would be expected. It was previously removed in #7934 for that same reason, but was reintroduced for unclear reasons in a later refactoring
1 parent aaa9cd0 commit 4b365f6

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ Deprecations
781781
- The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`)
782782
- The ``tz`` keyword in :meth:`Period.to_timestamp` is deprecated and will be removed in a future version; use `per.to_timestamp(...).tz_localize(tz)`` instead (:issue:`34522`)
783783
- :meth:`DatetimeIndex.to_perioddelta` is deprecated and will be removed in a future version. Use ``index - index.to_period(freq).to_timestamp()`` instead (:issue:`34853`)
784+
- the ``center`` keyword in the :meth:`DataFrame.expanding` function is deprecated and will be removed in a future version (:issue:`20647`)
784785

785786
.. ---------------------------------------------------------------------------
786787

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10501,7 +10501,7 @@ def rolling(
1050110501
cls.rolling = rolling
1050210502

1050310503
@doc(Expanding)
10504-
def expanding(self, min_periods=1, center=False, axis=0):
10504+
def expanding(self, min_periods=1, center=None, axis=0):
1050510505
axis = self._get_axis_number(axis)
1050610506
return Expanding(self, min_periods=min_periods, center=center, axis=axis)
1050710507

pandas/core/window/expanding.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from textwrap import dedent
23
from typing import Dict, Optional
34

@@ -57,7 +58,19 @@ class Expanding(_Rolling_and_Expanding):
5758

5859
_attributes = ["min_periods", "center", "axis"]
5960

60-
def __init__(self, obj, min_periods=1, center=False, axis=0, **kwargs):
61+
def __init__(self, obj, min_periods=1, center=None, axis=0, **kwargs):
62+
if center is not None:
63+
warnings.warn(
64+
"""
65+
the `center` argument on `expanding` will be removed in the future.
66+
Please be aware that there are issues with its current implementation,
67+
and use it with caution
68+
(https://github.com/pandas-dev/pandas/issues/20647)""",
69+
FutureWarning,
70+
stacklevel=3,
71+
)
72+
else:
73+
center = False
6174
super().__init__(obj=obj, min_periods=min_periods, center=center, axis=axis)
6275

6376
@property

pandas/tests/window/test_expanding.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,16 @@ def test_iter_expanding_series(ser, expected, min_periods):
210210

211211
for (expected, actual) in zip(expected, ser.expanding(min_periods)):
212212
tm.assert_series_equal(actual, expected)
213+
214+
215+
def test_center_deprecate_warning():
216+
# GH 20647
217+
df = pd.DataFrame()
218+
with tm.assert_produces_warning(FutureWarning):
219+
df.expanding(center=True)
220+
221+
with tm.assert_produces_warning(FutureWarning):
222+
df.expanding(center=False)
223+
224+
with tm.assert_produces_warning(None):
225+
df.expanding()

0 commit comments

Comments
 (0)