Skip to content

Commit

Permalink
Deprecate center on df.expanding (pandas-dev#34887)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBrouns authored and fangchenli committed Jul 16, 2020
1 parent dcb97ba commit 03296ec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,9 @@ Deprecations
precision through the ``rtol``, and ``atol`` parameters, thus deprecating the
``check_less_precise`` parameter. (:issue:`13357`).
- :func:`DataFrame.melt` accepting a value_name that already exists is deprecated, and will be removed in a future version (:issue:`34731`)
- the ``center`` keyword in the :meth:`DataFrame.expanding` function is deprecated and will be removed in a future version (:issue:`20647`)



.. ---------------------------------------------------------------------------
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10500,8 +10500,18 @@ def rolling(
cls.rolling = rolling

@doc(Expanding)
def expanding(self, min_periods=1, center=False, axis=0):
def expanding(self, min_periods=1, center=None, axis=0):
axis = self._get_axis_number(axis)
if center is not None:
warnings.warn(
"The `center` argument on `expanding` "
"will be removed in the future",
FutureWarning,
stacklevel=2,
)
else:
center = False

return Expanding(self, min_periods=min_periods, center=center, axis=axis)

cls.expanding = expanding
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Expanding(_Rolling_and_Expanding):

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

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

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def test_expanding_corr_pairwise(frame):
ids=["sum", "mean", "max", "min"],
)
def test_expanding_func(func, static_comp, has_min_periods, series, frame, nan_locs):
def expanding_func(x, min_periods=1, center=False, axis=0):
exp = x.expanding(min_periods=min_periods, center=center, axis=axis)
def expanding_func(x, min_periods=1, axis=0):
exp = x.expanding(min_periods=min_periods, axis=axis)
return getattr(exp, func)()

_check_expanding(
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_expanding_apply_consistency(

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning,
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning
)
# test consistency between expanding_xyz() and either (a)
# expanding_apply of Series.xyz(), or (b) expanding_apply of
Expand Down Expand Up @@ -267,7 +267,7 @@ def test_expanding_consistency(consistency_data, min_periods):
# with empty/0-length Series/DataFrames
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning,
"ignore", message=".*(empty slice|0 for slice).*", category=RuntimeWarning
)

# test consistency between different expanding_* moments
Expand Down Expand Up @@ -454,7 +454,7 @@ def test_expanding_cov_pairwise_diff_length():
def test_expanding_corr_pairwise_diff_length():
# GH 7512
df1 = DataFrame(
[[1, 2], [3, 2], [3, 4]], columns=["A", "B"], index=Index(range(3), name="bar"),
[[1, 2], [3, 2], [3, 4]], columns=["A", "B"], index=Index(range(3), name="bar")
)
df1a = DataFrame(
[[1, 2], [3, 4]], index=Index([0, 2], name="bar"), columns=["A", "B"]
Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ def test_agg():

with pytest.raises(SpecificationError, match=msg):
r.aggregate(
{
"A": {"mean": "mean", "sum": "sum"},
"B": {"mean2": "mean", "sum2": "sum"},
}
{"A": {"mean": "mean", "sum": "sum"}, "B": {"mean2": "mean", "sum2": "sum"}}
)

result = r.aggregate({"A": ["mean", "std"], "B": ["mean", "std"]})
Expand Down Expand Up @@ -191,11 +188,7 @@ def test_count_nonnumeric_types():
"dt_nat",
"periods_nat",
]
dt_nat_col = [
Timestamp("20170101"),
Timestamp("20170203"),
Timestamp(None),
]
dt_nat_col = [Timestamp("20170101"), Timestamp("20170203"), Timestamp(None)]

df = DataFrame(
{
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/window/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def test_doc_string():
df.expanding(2).sum()


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

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

for (expected, actual) in zip(expected, ser.expanding(min_periods)):
tm.assert_series_equal(actual, expected)


def test_center_deprecate_warning():
# GH 20647
df = pd.DataFrame()
with tm.assert_produces_warning(FutureWarning):
df.expanding(center=True)

with tm.assert_produces_warning(FutureWarning):
df.expanding(center=False)

with tm.assert_produces_warning(None):
df.expanding()

0 comments on commit 03296ec

Please sign in to comment.