Skip to content

DEPR: DataFrame.resample axis parameter #51901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Deprecations
- Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`)
- Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`)
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
- Deprecated ``axis=1`` in :meth:`DataFrame.resample`, do ``frame.T.resample(...)`` instead (:issue:`51778`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11408,7 +11408,7 @@ def asfreq(
def resample(
self,
rule,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
closed: str | None = None,
label: str | None = None,
convention: str = "start",
Expand Down
89 changes: 81 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8531,7 +8531,7 @@ def between_time(
def resample(
self,
rule,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
closed: str | None = None,
label: str | None = None,
convention: str = "start",
Expand All @@ -8558,6 +8558,8 @@ def resample(
Which axis to use for up- or down-sampling. For `Series` this parameter
is unused and defaults to 0. Must be
`DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.
.. deprecated:: 2.0.0
Use frame.T.resample(...) instead.
closed : {{'right', 'left'}}, default None
Which side of bin interval is closed. The default is 'left'
for all frequency offsets except for 'M', 'A', 'Q', 'BM',
Expand Down Expand Up @@ -8911,7 +8913,25 @@ def resample(
"""
from pandas.core.resample import get_resampler

axis = self._get_axis_number(axis)
if axis is not lib.no_default:
axis = self._get_axis_number(axis)
if axis == 1:
warnings.warn(
"DataFrame.resample with axis=1 is deprecated. Do "
"`frame.T.resample(...)` without axis instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
"The 'axis' keyword in DataFrame.resample is deprecated and "
"will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

return get_resampler(
cast("Series | DataFrame", self),
freq=rule,
Expand Down Expand Up @@ -11891,12 +11911,29 @@ def rolling(
center: bool_t = False,
win_type: str | None = None,
on: str | None = None,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
closed: str | None = None,
step: int | None = None,
method: str = "single",
) -> Window | Rolling:
axis = self._get_axis_number(axis)
if axis is not lib.no_default:
axis = self._get_axis_number(axis)
if axis == 1:
warnings.warn(
"DataFrame.rolling with axis=1 is deprecated. Do "
"`frame.T.rolling(...)` without axis instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
"The 'axis' keyword in DataFrame.rolling is deprecated and "
"will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

if win_type is not None:
return Window(
Expand Down Expand Up @@ -11930,10 +11967,28 @@ def rolling(
def expanding(
self,
min_periods: int = 1,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
method: str = "single",
) -> Expanding:
axis = self._get_axis_number(axis)
if axis is not lib.no_default:
axis = self._get_axis_number(axis)
if axis == 1:
warnings.warn(
"DataFrame.expanding with axis=1 is deprecated. Do "
"`frame.T.expanding(...)` without axis instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
"The 'axis' keyword in DataFrame.expanding is deprecated and "
"will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

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

@final
Expand All @@ -11947,11 +12002,29 @@ def ewm(
min_periods: int | None = 0,
adjust: bool_t = True,
ignore_na: bool_t = False,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
times: np.ndarray | DataFrame | Series | None = None,
method: str = "single",
) -> ExponentialMovingWindow:
axis = self._get_axis_number(axis)
if axis is not lib.no_default:
axis = self._get_axis_number(axis)
if axis == 1:
warnings.warn(
"DataFrame.ewm with axis=1 is deprecated. Do "
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one query here -

  1. should we make this message separate for DataFrame vs Series
  2. or use a common generic message without usage of the words DataFrame/Series

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use f"{type(self).__name__}.ewm with ..."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice ! will update the PR

"`frame.T.ewm(...)` without axis instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
"The 'axis' keyword in DataFrame.ewm is deprecated and "
"will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

return ExponentialMovingWindow(
self,
com=com,
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5570,7 +5570,7 @@ def asfreq(
def resample(
self,
rule,
axis: Axis = 0,
axis: Axis | lib.NoDefault = lib.no_default,
closed: str | None = None,
label: str | None = None,
convention: str = "start",
Expand All @@ -5581,6 +5581,14 @@ def resample(
offset: TimedeltaConvertibleTypes | None = None,
group_keys: bool | lib.NoDefault = no_default,
) -> Resampler:
if axis is not lib.no_default:
warnings.warn(
"Series resample axis keyword is deprecated and will be removed in a "
"future version.",
FutureWarning,
stacklevel=find_stack_level(),
)

return super().resample(
rule=rule,
axis=axis,
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,10 @@ def test_resample_dup_index():
columns=[Period(year=2000, month=i + 1, freq="M") for i in range(12)],
)
df.iloc[3, :] = np.nan
result = df.resample("Q", axis=1).mean()
warning_msg = "DataFrame.resample with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
result = df.resample("Q", axis=1).mean()

msg = "DataFrame.groupby with axis=1 is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean()
Expand Down Expand Up @@ -729,7 +732,9 @@ def test_resample_axis1(unit):
rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
df = DataFrame(np.random.randn(3, len(rng)), columns=rng, index=["a", "b", "c"])

result = df.resample("M", axis=1).mean()
warning_msg = "DataFrame.resample with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
result = df.resample("M", axis=1).mean()
expected = df.T.resample("M").mean().T
tm.assert_frame_equal(result, expected)

Expand Down
40 changes: 37 additions & 3 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,13 @@ def test_multi_agg_axis_1_raises(func):
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
index.name = "date"
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T
res = df.resample("M", axis=1)
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
res.agg(func)
warning_msg = "DataFrame.resample with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
res = df.resample("M", axis=1)
with pytest.raises(
NotImplementedError, match="axis other than 0 is not supported"
):
res.agg(func)


def test_agg_nested_dicts():
Expand Down Expand Up @@ -973,3 +977,33 @@ def test_args_kwargs_depr(method, raises):
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(TypeError, match=error_msg_type):
func(*args, 1, 2, 3)


def test_df_axis_param_depr():
np.random.seed(1234)
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
index.name = "date"
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T

# Deprication error when axis=1 is explicitly passed
warning_msg = "DataFrame.resample with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
df.resample("M", axis=1)

# Deprication error when axis=0 is explicitly passed
df = df.T
warning_msg = (
"The 'axis' keyword in DataFrame.resample is deprecated and "
"will be removed in a future version."
)
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
df.resample("M", axis=0)


def test_series_axis_param_depr():
warning_msg = (
"Series resample axis keyword is deprecated and will be removed in a "
"future version."
)
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
test_series.resample("H", axis=0)
28 changes: 20 additions & 8 deletions pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ def test_agg(step):
def test_multi_axis_1_raises(func):
# GH#46904
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
r = df.rolling(window=3, axis=1)
warning_msg = "DataFrame.rolling with axis=1 is deprecated."

with tm.assert_produces_warning(FutureWarning, match=warning_msg):
r = df.rolling(window=3, axis=1)
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
r.agg(func)

Expand Down Expand Up @@ -344,20 +347,29 @@ def test_dont_modify_attributes_after_methods(

def test_centered_axis_validation(step):
# ok
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
axis_0_warning_msg = (
"The 'axis' keyword in DataFrame.rolling is deprecated and "
"will be removed in a future version."
)
axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated."

with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg):
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()

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

# ok ok
DataFrame(np.ones((10, 10))).rolling(
window=3, center=True, axis=0, step=step
).mean()
DataFrame(np.ones((10, 10))).rolling(
window=3, center=True, axis=1, step=step
).mean()
with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg):
DataFrame(np.ones((10, 10))).rolling(
window=3, center=True, axis=0, step=step
).mean()
with tm.assert_produces_warning(FutureWarning, match=axis_1_warning_msg):
DataFrame(np.ones((10, 10))).rolling(
window=3, center=True, axis=1, step=step
).mean()

# bad axis
msg = "No axis named 2 for object type DataFrame"
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/window/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ def test_center_reindex_frame(raw, frame):
def test_axis1(raw):
# GH 45912
df = DataFrame([1, 2])
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
warning_msg = "DataFrame.rolling with axis=1 is deprecated."

with tm.assert_produces_warning(FutureWarning, match=warning_msg):
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
expected = DataFrame([1.0, 2.0])
tm.assert_frame_equal(result, expected)
19 changes: 18 additions & 1 deletion pandas/tests/window/test_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype):
df = DataFrame(
{0: range(5), 1: range(6, 11), 2: range(10, 20, 2)}, dtype=float_numpy_dtype
)
e = df.ewm(alpha=0.5, axis=1)
warning_msg = "DataFrame.ewm with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
e = df.ewm(alpha=0.5, axis=1)
result = getattr(e, func)()

tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -719,3 +721,18 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype):
op2 = getattr(ewm2, kernel)
expected = op2(*arg2, numeric_only=numeric_only)
tm.assert_series_equal(result, expected)


def test_df_ewn_axis_param_depr():
df = DataFrame({"a": [1], "b": 2, "c": 3})

warning_msg = (
"The 'axis' keyword in DataFrame.ewm is deprecated and "
"will be removed in a future version."
)
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
df.ewm(span=2, axis=0)

warning_msg = "DataFrame.ewm with axis=1 is deprecated."
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
df.ewm(span=2, axis=1)
Loading