Skip to content

Commit 1418ce8

Browse files
authored
Deprecate center on df.expanding (#34887)
1 parent 84e83b1 commit 1418ce8

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

doc/source/whatsnew/v1.1.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,9 @@ Deprecations
825825
precision through the ``rtol``, and ``atol`` parameters, thus deprecating the
826826
``check_less_precise`` parameter. (:issue:`13357`).
827827
- :func:`DataFrame.melt` accepting a value_name that already exists is deprecated, and will be removed in a future version (:issue:`34731`)
828+
- the ``center`` keyword in the :meth:`DataFrame.expanding` function is deprecated and will be removed in a future version (:issue:`20647`)
829+
830+
828831

829832
.. ---------------------------------------------------------------------------
830833

pandas/core/generic.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -10500,8 +10500,18 @@ def rolling(
1050010500
cls.rolling = rolling
1050110501

1050210502
@doc(Expanding)
10503-
def expanding(self, min_periods=1, center=False, axis=0):
10503+
def expanding(self, min_periods=1, center=None, axis=0):
1050410504
axis = self._get_axis_number(axis)
10505+
if center is not None:
10506+
warnings.warn(
10507+
"The `center` argument on `expanding` "
10508+
"will be removed in the future",
10509+
FutureWarning,
10510+
stacklevel=2,
10511+
)
10512+
else:
10513+
center = False
10514+
1050510515
return Expanding(self, min_periods=min_periods, center=center, axis=axis)
1050610516

1050710517
cls.expanding = expanding

pandas/core/window/expanding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Expanding(_Rolling_and_Expanding):
5757

5858
_attributes = ["min_periods", "center", "axis"]
5959

60-
def __init__(self, obj, min_periods=1, center=False, axis=0, **kwargs):
60+
def __init__(self, obj, min_periods=1, center=None, axis=0, **kwargs):
6161
super().__init__(obj=obj, min_periods=min_periods, center=center, axis=axis)
6262

6363
@property

pandas/tests/window/moments/test_moments_consistency_expanding.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def test_expanding_corr_pairwise(frame):
119119
ids=["sum", "mean", "max", "min"],
120120
)
121121
def test_expanding_func(func, static_comp, has_min_periods, series, frame, nan_locs):
122-
def expanding_func(x, min_periods=1, center=False, axis=0):
123-
exp = x.expanding(min_periods=min_periods, center=center, axis=axis)
122+
def expanding_func(x, min_periods=1, axis=0):
123+
exp = x.expanding(min_periods=min_periods, axis=axis)
124124
return getattr(exp, func)()
125125

126126
_check_expanding(
@@ -166,7 +166,7 @@ def test_expanding_apply_consistency(
166166

167167
with warnings.catch_warnings():
168168
warnings.filterwarnings(
169-
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning,
169+
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning
170170
)
171171
# test consistency between expanding_xyz() and either (a)
172172
# expanding_apply of Series.xyz(), or (b) expanding_apply of
@@ -267,7 +267,7 @@ def test_expanding_consistency(consistency_data, min_periods):
267267
# with empty/0-length Series/DataFrames
268268
with warnings.catch_warnings():
269269
warnings.filterwarnings(
270-
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning,
270+
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning
271271
)
272272

273273
# test consistency between different expanding_* moments
@@ -454,7 +454,7 @@ def test_expanding_cov_pairwise_diff_length():
454454
def test_expanding_corr_pairwise_diff_length():
455455
# GH 7512
456456
df1 = DataFrame(
457-
[[1, 2], [3, 2], [3, 4]], columns=["A", "B"], index=Index(range(3), name="bar"),
457+
[[1, 2], [3, 2], [3, 4]], columns=["A", "B"], index=Index(range(3), name="bar")
458458
)
459459
df1a = DataFrame(
460460
[[1, 2], [3, 4]], index=Index([0, 2], name="bar"), columns=["A", "B"]

pandas/tests/window/test_api.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ def test_agg():
107107

108108
with pytest.raises(SpecificationError, match=msg):
109109
r.aggregate(
110-
{
111-
"A": {"mean": "mean", "sum": "sum"},
112-
"B": {"mean2": "mean", "sum2": "sum"},
113-
}
110+
{"A": {"mean": "mean", "sum": "sum"}, "B": {"mean2": "mean", "sum2": "sum"}}
114111
)
115112

116113
result = r.aggregate({"A": ["mean", "std"], "B": ["mean", "std"]})
@@ -191,11 +188,7 @@ def test_count_nonnumeric_types():
191188
"dt_nat",
192189
"periods_nat",
193190
]
194-
dt_nat_col = [
195-
Timestamp("20170101"),
196-
Timestamp("20170203"),
197-
Timestamp(None),
198-
]
191+
dt_nat_col = [Timestamp("20170101"), Timestamp("20170203"), Timestamp(None)]
199192

200193
df = DataFrame(
201194
{

pandas/tests/window/test_expanding.py

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def test_doc_string():
1616
df.expanding(2).sum()
1717

1818

19+
@pytest.mark.filterwarnings(
20+
"ignore:The `center` argument on `expanding` will be removed in the future"
21+
)
1922
def test_constructor(which):
2023
# GH 12669
2124

@@ -213,3 +216,16 @@ def test_iter_expanding_series(ser, expected, min_periods):
213216

214217
for (expected, actual) in zip(expected, ser.expanding(min_periods)):
215218
tm.assert_series_equal(actual, expected)
219+
220+
221+
def test_center_deprecate_warning():
222+
# GH 20647
223+
df = pd.DataFrame()
224+
with tm.assert_produces_warning(FutureWarning):
225+
df.expanding(center=True)
226+
227+
with tm.assert_produces_warning(FutureWarning):
228+
df.expanding(center=False)
229+
230+
with tm.assert_produces_warning(None):
231+
df.expanding()

0 commit comments

Comments
 (0)