Skip to content

Commit d22d1f2

Browse files
authored
DEPR: DataFrame.resample axis parameter (#51901)
* deprication of resample * deprication of resample * deprication of resample * deprication of resample * deprication of resample * pre commit fixes * updated testcases & whatsnew * added more testcases * addressing review comments * deprication of axis param in rolling functions * deprication of axis param in rolling functions * deprication of axis param in rolling functions - addressing review comments * revert changes * revert changes * revert changes * revert changes
1 parent 540db96 commit d22d1f2

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Deprecations
110110
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
111111
- Deprecated ``axis=1`` in :meth:`DataFrame.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`)
112112
- Deprecated the ``axis`` keyword in :meth:`DataFrame.ewm`, :meth:`Series.ewm`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, :meth:`Series.expanding` (:issue:`51778`)
113+
- Deprecated the ``axis`` keyword in :meth:`DataFrame.resample`, :meth:`Series.resample` (:issue:`51778`)
113114
- Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`)
114115
- Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`)
115116
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11418,7 +11418,7 @@ def asfreq(
1141811418
def resample(
1141911419
self,
1142011420
rule,
11421-
axis: Axis = 0,
11421+
axis: Axis | lib.NoDefault = lib.no_default,
1142211422
closed: str | None = None,
1142311423
label: str | None = None,
1142411424
convention: str = "start",

pandas/core/generic.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -8555,7 +8555,7 @@ def between_time(
85558555
def resample(
85568556
self,
85578557
rule,
8558-
axis: Axis = 0,
8558+
axis: Axis | lib.NoDefault = lib.no_default,
85598559
closed: str | None = None,
85608560
label: str | None = None,
85618561
convention: str = "start",
@@ -8582,6 +8582,9 @@ def resample(
85828582
Which axis to use for up- or down-sampling. For `Series` this parameter
85838583
is unused and defaults to 0. Must be
85848584
`DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.
8585+
8586+
.. deprecated:: 2.0.0
8587+
Use frame.T.resample(...) instead.
85858588
closed : {{'right', 'left'}}, default None
85868589
Which side of bin interval is closed. The default is 'left'
85878590
for all frequency offsets except for 'M', 'A', 'Q', 'BM',
@@ -8938,7 +8941,25 @@ def resample(
89388941
"""
89398942
from pandas.core.resample import get_resampler
89408943

8941-
axis = self._get_axis_number(axis)
8944+
if axis is not lib.no_default:
8945+
axis = self._get_axis_number(axis)
8946+
if axis == 1:
8947+
warnings.warn(
8948+
"DataFrame.resample with axis=1 is deprecated. Do "
8949+
"`frame.T.resample(...)` without axis instead.",
8950+
FutureWarning,
8951+
stacklevel=find_stack_level(),
8952+
)
8953+
else:
8954+
warnings.warn(
8955+
"The 'axis' keyword in DataFrame.resample is deprecated and "
8956+
"will be removed in a future version.",
8957+
FutureWarning,
8958+
stacklevel=find_stack_level(),
8959+
)
8960+
else:
8961+
axis = 0
8962+
89428963
return get_resampler(
89438964
cast("Series | DataFrame", self),
89448965
freq=rule,

pandas/core/series.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5570,7 +5570,7 @@ def asfreq(
55705570
def resample(
55715571
self,
55725572
rule,
5573-
axis: Axis = 0,
5573+
axis: Axis | lib.NoDefault = lib.no_default,
55745574
closed: str | None = None,
55755575
label: str | None = None,
55765576
convention: str = "start",
@@ -5581,6 +5581,14 @@ def resample(
55815581
offset: TimedeltaConvertibleTypes | None = None,
55825582
group_keys: bool = False,
55835583
) -> Resampler:
5584+
if axis is not lib.no_default:
5585+
warnings.warn(
5586+
"Series resample axis keyword is deprecated and will be removed in a "
5587+
"future version.",
5588+
FutureWarning,
5589+
stacklevel=find_stack_level(),
5590+
)
5591+
55845592
return super().resample(
55855593
rule=rule,
55865594
axis=axis,

pandas/tests/resample/test_datetime_index.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,10 @@ def test_resample_dup_index():
641641
columns=[Period(year=2000, month=i + 1, freq="M") for i in range(12)],
642642
)
643643
df.iloc[3, :] = np.nan
644-
result = df.resample("Q", axis=1).mean()
644+
warning_msg = "DataFrame.resample with axis=1 is deprecated."
645+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
646+
result = df.resample("Q", axis=1).mean()
647+
645648
msg = "DataFrame.groupby with axis=1 is deprecated"
646649
with tm.assert_produces_warning(FutureWarning, match=msg):
647650
expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean()
@@ -729,7 +732,9 @@ def test_resample_axis1(unit):
729732
rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
730733
df = DataFrame(np.random.randn(3, len(rng)), columns=rng, index=["a", "b", "c"])
731734

732-
result = df.resample("M", axis=1).mean()
735+
warning_msg = "DataFrame.resample with axis=1 is deprecated."
736+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
737+
result = df.resample("M", axis=1).mean()
733738
expected = df.T.resample("M").mean().T
734739
tm.assert_frame_equal(result, expected)
735740

pandas/tests/resample/test_resample_api.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,13 @@ def test_multi_agg_axis_1_raises(func):
568568
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
569569
index.name = "date"
570570
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T
571-
res = df.resample("M", axis=1)
572-
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
573-
res.agg(func)
571+
warning_msg = "DataFrame.resample with axis=1 is deprecated."
572+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
573+
res = df.resample("M", axis=1)
574+
with pytest.raises(
575+
NotImplementedError, match="axis other than 0 is not supported"
576+
):
577+
res.agg(func)
574578

575579

576580
def test_agg_nested_dicts():
@@ -971,3 +975,33 @@ def test_args_kwargs_depr(method, raises):
971975
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
972976
with pytest.raises(TypeError, match=error_msg_type):
973977
func(*args, 1, 2, 3)
978+
979+
980+
def test_df_axis_param_depr():
981+
np.random.seed(1234)
982+
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
983+
index.name = "date"
984+
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T
985+
986+
# Deprication error when axis=1 is explicitly passed
987+
warning_msg = "DataFrame.resample with axis=1 is deprecated."
988+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
989+
df.resample("M", axis=1)
990+
991+
# Deprication error when axis=0 is explicitly passed
992+
df = df.T
993+
warning_msg = (
994+
"The 'axis' keyword in DataFrame.resample is deprecated and "
995+
"will be removed in a future version."
996+
)
997+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
998+
df.resample("M", axis=0)
999+
1000+
1001+
def test_series_axis_param_depr():
1002+
warning_msg = (
1003+
"Series resample axis keyword is deprecated and will be removed in a "
1004+
"future version."
1005+
)
1006+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
1007+
test_series.resample("H", axis=0)

0 commit comments

Comments
 (0)