Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
24 changes: 22 additions & 2 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
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
24 changes: 15 additions & 9 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,16 @@ 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()
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()
expected.columns = [Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)]
tm.assert_frame_equal(result, expected)
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()
expected.columns = [
Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)
]
tm.assert_frame_equal(result, expected)


def test_resample_reresample(unit):
Expand Down Expand Up @@ -729,9 +733,11 @@ 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()
expected = df.T.resample("M").mean().T
tm.assert_frame_equal(result, expected)
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)


@pytest.mark.parametrize("freq", ["t", "5t", "15t", "30t", "4h", "12h"])
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)