Skip to content

Commit 9d8334e

Browse files
committed
deprication of axis param in rolling functions
1 parent d130495 commit 9d8334e

File tree

8 files changed

+215
-38
lines changed

8 files changed

+215
-38
lines changed

pandas/core/generic.py

+59-6
Original file line numberDiff line numberDiff line change
@@ -11911,12 +11911,29 @@ def rolling(
1191111911
center: bool_t = False,
1191211912
win_type: str | None = None,
1191311913
on: str | None = None,
11914-
axis: Axis = 0,
11914+
axis: Axis | lib.NoDefault = lib.no_default,
1191511915
closed: str | None = None,
1191611916
step: int | None = None,
1191711917
method: str = "single",
1191811918
) -> Window | Rolling:
11919-
axis = self._get_axis_number(axis)
11919+
if axis is not lib.no_default:
11920+
axis = self._get_axis_number(axis)
11921+
if axis == 1:
11922+
warnings.warn(
11923+
"DataFrame.rolling with axis=1 is deprecated. Do "
11924+
"`frame.T.rolling(...)` without axis instead.",
11925+
FutureWarning,
11926+
stacklevel=find_stack_level(),
11927+
)
11928+
else:
11929+
warnings.warn(
11930+
"The 'axis' keyword in DataFrame.rolling is deprecated and "
11931+
"will be removed in a future version.",
11932+
FutureWarning,
11933+
stacklevel=find_stack_level(),
11934+
)
11935+
else:
11936+
axis = 0
1192011937

1192111938
if win_type is not None:
1192211939
return Window(
@@ -11950,10 +11967,28 @@ def rolling(
1195011967
def expanding(
1195111968
self,
1195211969
min_periods: int = 1,
11953-
axis: Axis = 0,
11970+
axis: Axis | lib.NoDefault = lib.no_default,
1195411971
method: str = "single",
1195511972
) -> Expanding:
11956-
axis = self._get_axis_number(axis)
11973+
if axis is not lib.no_default:
11974+
axis = self._get_axis_number(axis)
11975+
if axis == 1:
11976+
warnings.warn(
11977+
"DataFrame.expanding with axis=1 is deprecated. Do "
11978+
"`frame.T.expanding(...)` without axis instead.",
11979+
FutureWarning,
11980+
stacklevel=find_stack_level(),
11981+
)
11982+
else:
11983+
warnings.warn(
11984+
"The 'axis' keyword in DataFrame.expanding is deprecated and "
11985+
"will be removed in a future version.",
11986+
FutureWarning,
11987+
stacklevel=find_stack_level(),
11988+
)
11989+
else:
11990+
axis = 0
11991+
1195711992
return Expanding(self, min_periods=min_periods, axis=axis, method=method)
1195811993

1195911994
@final
@@ -11967,11 +12002,29 @@ def ewm(
1196712002
min_periods: int | None = 0,
1196812003
adjust: bool_t = True,
1196912004
ignore_na: bool_t = False,
11970-
axis: Axis = 0,
12005+
axis: Axis | lib.NoDefault = lib.no_default,
1197112006
times: np.ndarray | DataFrame | Series | None = None,
1197212007
method: str = "single",
1197312008
) -> ExponentialMovingWindow:
11974-
axis = self._get_axis_number(axis)
12009+
if axis is not lib.no_default:
12010+
axis = self._get_axis_number(axis)
12011+
if axis == 1:
12012+
warnings.warn(
12013+
"DataFrame.ewm with axis=1 is deprecated. Do "
12014+
"`frame.T.ewm(...)` without axis instead.",
12015+
FutureWarning,
12016+
stacklevel=find_stack_level(),
12017+
)
12018+
else:
12019+
warnings.warn(
12020+
"The 'axis' keyword in DataFrame.ewm is deprecated and "
12021+
"will be removed in a future version.",
12022+
FutureWarning,
12023+
stacklevel=find_stack_level(),
12024+
)
12025+
else:
12026+
axis = 0
12027+
1197512028
return ExponentialMovingWindow(
1197612029
self,
1197712030
com=com,

pandas/tests/window/test_api.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ def test_agg(step):
129129
def test_multi_axis_1_raises(func):
130130
# GH#46904
131131
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
132-
r = df.rolling(window=3, axis=1)
132+
warning_msg = "DataFrame.rolling with axis=1 is deprecated."
133+
134+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
135+
r = df.rolling(window=3, axis=1)
133136
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
134137
r.agg(func)
135138

@@ -344,20 +347,29 @@ def test_dont_modify_attributes_after_methods(
344347

345348
def test_centered_axis_validation(step):
346349
# ok
347-
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
350+
axis_0_warning_msg = (
351+
"The 'axis' keyword in DataFrame.rolling is deprecated and "
352+
"will be removed in a future version."
353+
)
354+
axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated."
355+
356+
with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg):
357+
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
348358

349359
# bad axis
350360
msg = "No axis named 1 for object type Series"
351361
with pytest.raises(ValueError, match=msg):
352362
Series(np.ones(10)).rolling(window=3, center=True, axis=1, step=step).mean()
353363

354364
# ok ok
355-
DataFrame(np.ones((10, 10))).rolling(
356-
window=3, center=True, axis=0, step=step
357-
).mean()
358-
DataFrame(np.ones((10, 10))).rolling(
359-
window=3, center=True, axis=1, step=step
360-
).mean()
365+
with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg):
366+
DataFrame(np.ones((10, 10))).rolling(
367+
window=3, center=True, axis=0, step=step
368+
).mean()
369+
with tm.assert_produces_warning(FutureWarning, match=axis_1_warning_msg):
370+
DataFrame(np.ones((10, 10))).rolling(
371+
window=3, center=True, axis=1, step=step
372+
).mean()
361373

362374
# bad axis
363375
msg = "No axis named 2 for object type DataFrame"

pandas/tests/window/test_apply.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ def test_center_reindex_frame(raw, frame):
321321
def test_axis1(raw):
322322
# GH 45912
323323
df = DataFrame([1, 2])
324-
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
324+
warning_msg = "DataFrame.rolling with axis=1 is deprecated."
325+
326+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
327+
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
325328
expected = DataFrame([1.0, 2.0])
326329
tm.assert_frame_equal(result, expected)

pandas/tests/window/test_ewm.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype):
198198
df = DataFrame(
199199
{0: range(5), 1: range(6, 11), 2: range(10, 20, 2)}, dtype=float_numpy_dtype
200200
)
201-
e = df.ewm(alpha=0.5, axis=1)
201+
warning_msg = "DataFrame.ewm with axis=1 is deprecated."
202+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
203+
e = df.ewm(alpha=0.5, axis=1)
202204
result = getattr(e, func)()
203205

204206
tm.assert_frame_equal(result, expected)
@@ -719,3 +721,18 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype):
719721
op2 = getattr(ewm2, kernel)
720722
expected = op2(*arg2, numeric_only=numeric_only)
721723
tm.assert_series_equal(result, expected)
724+
725+
726+
def test_df_ewn_axis_param_depr():
727+
df = DataFrame({"a": [1], "b": 2, "c": 3})
728+
729+
warning_msg = (
730+
"The 'axis' keyword in DataFrame.ewm is deprecated and "
731+
"will be removed in a future version."
732+
)
733+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
734+
df.ewm(span=2, axis=0)
735+
736+
warning_msg = "DataFrame.ewm with axis=1 is deprecated."
737+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
738+
df.ewm(span=2, axis=1)

pandas/tests/window/test_expanding.py

+44-10
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,22 @@ def test_missing_minp_zero():
8282
def test_expanding_axis(axis_frame):
8383
# see gh-23372.
8484
df = DataFrame(np.ones((10, 20)))
85-
axis = df._get_axis_number(axis_frame)
86-
85+
axis = df._get_axis_number(axis_frame)
8786
if axis == 0:
8887
expected = DataFrame(
8988
{i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)}
9089
)
90+
warning_msg = (
91+
"The 'axis' keyword in DataFrame.expanding is deprecated and "
92+
"will be removed in a future version."
93+
)
9194
else:
9295
# axis == 1
9396
expected = DataFrame([[np.nan] * 2 + [float(i) for i in range(3, 21)]] * 10)
97+
warning_msg = "DataFrame.expanding with axis=1 is deprecated."
9498

95-
result = df.expanding(3, axis=axis_frame).sum()
99+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
100+
result = df.expanding(3, axis=axis_frame).sum()
96101
tm.assert_frame_equal(result, expected)
97102

98103

@@ -323,7 +328,12 @@ def test_expanding_corr_pairwise(frame):
323328
)
324329
def test_expanding_func(func, static_comp, frame_or_series):
325330
data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10))
326-
result = getattr(data.expanding(min_periods=1, axis=0), func)()
331+
warning_msg = (
332+
"The 'axis' keyword in DataFrame.expanding is deprecated and "
333+
"will be removed in a future version."
334+
)
335+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
336+
result = getattr(data.expanding(min_periods=1, axis=0), func)()
327337
assert isinstance(result, frame_or_series)
328338

329339
expected = static_comp(data[:11])
@@ -341,26 +351,36 @@ def test_expanding_func(func, static_comp, frame_or_series):
341351
def test_expanding_min_periods(func, static_comp):
342352
ser = Series(np.random.randn(50))
343353

344-
result = getattr(ser.expanding(min_periods=30, axis=0), func)()
354+
warning_msg = (
355+
"The 'axis' keyword in DataFrame.expanding is deprecated and "
356+
"will be removed in a future version."
357+
)
358+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
359+
result = getattr(ser.expanding(min_periods=30, axis=0), func)()
345360
assert result[:29].isna().all()
346361
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
347362

348363
# min_periods is working correctly
349-
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
364+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
365+
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
350366
assert isna(result.iloc[13])
351367
assert notna(result.iloc[14])
352368

353369
ser2 = Series(np.random.randn(20))
354-
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
370+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
371+
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
355372
assert isna(result[3])
356373
assert notna(result[4])
357374

358375
# min_periods=0
359-
result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
360-
result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
376+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
377+
result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
378+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
379+
result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
361380
tm.assert_almost_equal(result0, result1)
362381

363-
result = getattr(ser.expanding(min_periods=1, axis=0), func)()
382+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
383+
result = getattr(ser.expanding(min_periods=1, axis=0), func)()
364384
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
365385

366386

@@ -693,3 +713,17 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype):
693713
op2 = getattr(expanding2, kernel)
694714
expected = op2(*arg2, numeric_only=numeric_only)
695715
tm.assert_series_equal(result, expected)
716+
717+
def test_df_expanding_axis_param_depr():
718+
df = DataFrame({"a": [1], "b": [2], "c": [3]})
719+
720+
warning_msg = (
721+
"The 'axis' keyword in DataFrame.expanding is deprecated and "
722+
"will be removed in a future version."
723+
)
724+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
725+
df.expanding(2, axis=0)
726+
727+
warning_msg = "DataFrame.expanding with axis=1 is deprecated."
728+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
729+
df.expanding(2, axis=1)

0 commit comments

Comments
 (0)