Skip to content

Commit f2cd62a

Browse files
authored
DEPR: DFGB.dtypes, rolling axis, Categorical.to_list (#51997)
* DEPR: DFGB.dtypes, rolling axis, Categorical.to_list * revert Categorical.to_list deprecation
1 parent 2171154 commit f2cd62a

File tree

12 files changed

+167
-48
lines changed

12 files changed

+167
-48
lines changed

doc/source/whatsnew/v2.1.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ Deprecations
101101
- Deprecated :meth:`DataFrame._data` and :meth:`Series._data`, use public APIs instead (:issue:`33333`)
102102
- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`)
103103
- Deprecated the default of ``observed=False`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby`; this will default to ``True`` in a future version (:issue:`43999`)
104+
- Deprecated :meth:`DataFrameGroupBy.dtypes`, check ``dtypes`` on the underlying object instead (:issue:`51045`)
104105
- Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`)
105106
- Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`)
106107
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
108+
- Deprecated ``axis=1`` in :meth:`DataFrame.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`)
109+
- 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`)
107110
- 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`)
108111
- 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`)
109112
-

pandas/core/apply.py

-4
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,6 @@ def columns(self) -> Index:
656656
def values(self):
657657
return self.obj.values
658658

659-
@cache_readonly
660-
def dtypes(self) -> Series:
661-
return self.obj.dtypes
662-
663659
def apply(self) -> DataFrame | Series:
664660
"""compute the results"""
665661
# dispatch to agg

pandas/core/generic.py

+67-6
Original file line numberDiff line numberDiff line change
@@ -11956,12 +11956,32 @@ def rolling(
1195611956
center: bool_t = False,
1195711957
win_type: str | None = None,
1195811958
on: str | None = None,
11959-
axis: Axis = 0,
11959+
axis: Axis | lib.NoDefault = lib.no_default,
1196011960
closed: str | None = None,
1196111961
step: int | None = None,
1196211962
method: str = "single",
1196311963
) -> Window | Rolling:
11964-
axis = self._get_axis_number(axis)
11964+
if axis is not lib.no_default:
11965+
axis = self._get_axis_number(axis)
11966+
name = "rolling"
11967+
if axis == 1:
11968+
warnings.warn(
11969+
f"Support for axis=1 in {type(self).__name__}.{name} is "
11970+
"deprecated and will be removed in a future version. "
11971+
f"Use obj.T.{name}(...) instead",
11972+
FutureWarning,
11973+
stacklevel=find_stack_level(),
11974+
)
11975+
else:
11976+
warnings.warn(
11977+
f"The 'axis' keyword in {type(self).__name__}.{name} is "
11978+
"deprecated and will be removed in a future version. "
11979+
"Call the method without the axis keyword instead.",
11980+
FutureWarning,
11981+
stacklevel=find_stack_level(),
11982+
)
11983+
else:
11984+
axis = 0
1196511985

1196611986
if win_type is not None:
1196711987
return Window(
@@ -11995,10 +12015,30 @@ def rolling(
1199512015
def expanding(
1199612016
self,
1199712017
min_periods: int = 1,
11998-
axis: Axis = 0,
12018+
axis: Axis | lib.NoDefault = lib.no_default,
1199912019
method: str = "single",
1200012020
) -> Expanding:
12001-
axis = self._get_axis_number(axis)
12021+
if axis is not lib.no_default:
12022+
axis = self._get_axis_number(axis)
12023+
name = "expanding"
12024+
if axis == 1:
12025+
warnings.warn(
12026+
f"Support for axis=1 in {type(self).__name__}.{name} is "
12027+
"deprecated and will be removed in a future version. "
12028+
f"Use obj.T.{name}(...) instead",
12029+
FutureWarning,
12030+
stacklevel=find_stack_level(),
12031+
)
12032+
else:
12033+
warnings.warn(
12034+
f"The 'axis' keyword in {type(self).__name__}.{name} is "
12035+
"deprecated and will be removed in a future version. "
12036+
"Call the method without the axis keyword instead.",
12037+
FutureWarning,
12038+
stacklevel=find_stack_level(),
12039+
)
12040+
else:
12041+
axis = 0
1200212042
return Expanding(self, min_periods=min_periods, axis=axis, method=method)
1200312043

1200412044
@final
@@ -12012,11 +12052,32 @@ def ewm(
1201212052
min_periods: int | None = 0,
1201312053
adjust: bool_t = True,
1201412054
ignore_na: bool_t = False,
12015-
axis: Axis = 0,
12055+
axis: Axis | lib.NoDefault = lib.no_default,
1201612056
times: np.ndarray | DataFrame | Series | None = None,
1201712057
method: str = "single",
1201812058
) -> ExponentialMovingWindow:
12019-
axis = self._get_axis_number(axis)
12059+
if axis is not lib.no_default:
12060+
axis = self._get_axis_number(axis)
12061+
name = "ewm"
12062+
if axis == 1:
12063+
warnings.warn(
12064+
f"Support for axis=1 in {type(self).__name__}.{name} is "
12065+
"deprecated and will be removed in a future version. "
12066+
f"Use obj.T.{name}(...) instead",
12067+
FutureWarning,
12068+
stacklevel=find_stack_level(),
12069+
)
12070+
else:
12071+
warnings.warn(
12072+
f"The 'axis' keyword in {type(self).__name__}.{name} is "
12073+
"deprecated and will be removed in a future version. "
12074+
"Call the method without the axis keyword instead.",
12075+
FutureWarning,
12076+
stacklevel=find_stack_level(),
12077+
)
12078+
else:
12079+
axis = 0
12080+
1202012081
return ExponentialMovingWindow(
1202112082
self,
1202212083
com=com,

pandas/core/groupby/generic.py

+8
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,14 @@ def hist(
26372637
@property
26382638
@doc(DataFrame.dtypes.__doc__)
26392639
def dtypes(self) -> Series:
2640+
# GH#51045
2641+
warnings.warn(
2642+
f"{type(self).__name__}.dtypes is deprecated and will be removed in "
2643+
"a future version. Check the dtypes on the base object instead",
2644+
FutureWarning,
2645+
stacklevel=find_stack_level(),
2646+
)
2647+
26402648
# error: Incompatible return value type (got "DataFrame", expected "Series")
26412649
return self.apply(lambda df: df.dtypes) # type: ignore[return-value]
26422650

pandas/tests/groupby/test_allowlist.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ def test_groupby_selection_other_methods(df):
274274

275275
# methods which aren't just .foo()
276276
tm.assert_frame_equal(g.fillna(0), g_exp.fillna(0))
277-
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
277+
msg = "DataFrameGroupBy.dtypes is deprecated"
278+
with tm.assert_produces_warning(FutureWarning, match=msg):
279+
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
278280
tm.assert_frame_equal(g.apply(lambda x: x.sum()), g_exp.apply(lambda x: x.sum()))
279281

280282
tm.assert_frame_equal(g.resample("D").mean(), g_exp.resample("D").mean())

pandas/tests/window/test_api.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ 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+
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
133+
with tm.assert_produces_warning(FutureWarning, match=msg):
134+
r = df.rolling(window=3, axis=1)
133135
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
134136
r.agg(func)
135137

@@ -344,29 +346,28 @@ def test_dont_modify_attributes_after_methods(
344346

345347
def test_centered_axis_validation(step):
346348
# ok
347-
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
349+
msg = "The 'axis' keyword in Series.rolling is deprecated"
350+
with tm.assert_produces_warning(FutureWarning, match=msg):
351+
Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
348352

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

354358
# 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()
359+
df = DataFrame(np.ones((10, 10)))
360+
msg = "The 'axis' keyword in DataFrame.rolling is deprecated"
361+
with tm.assert_produces_warning(FutureWarning, match=msg):
362+
df.rolling(window=3, center=True, axis=0, step=step).mean()
363+
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
364+
with tm.assert_produces_warning(FutureWarning, match=msg):
365+
df.rolling(window=3, center=True, axis=1, step=step).mean()
361366

362367
# bad axis
363368
msg = "No axis named 2 for object type DataFrame"
364369
with pytest.raises(ValueError, match=msg):
365-
(
366-
DataFrame(np.ones((10, 10)))
367-
.rolling(window=3, center=True, axis=2, step=step)
368-
.mean()
369-
)
370+
(df.rolling(window=3, center=True, axis=2, step=step).mean())
370371

371372

372373
def test_rolling_min_min_periods(step):

pandas/tests/window/test_apply.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ 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+
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
325+
with tm.assert_produces_warning(FutureWarning, match=msg):
326+
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
325327
expected = DataFrame([1.0, 2.0])
326328
tm.assert_frame_equal(result, expected)

pandas/tests/window/test_ewm.py

+3-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+
msg = "Support for axis=1 in DataFrame.ewm is deprecated"
202+
with tm.assert_produces_warning(FutureWarning, match=msg):
203+
e = df.ewm(alpha=0.5, axis=1)
202204
result = getattr(e, func)()
203205

204206
tm.assert_frame_equal(result, expected)

pandas/tests/window/test_expanding.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,17 @@ def test_expanding_axis(axis_frame):
8585
axis = df._get_axis_number(axis_frame)
8686

8787
if axis == 0:
88+
msg = "The 'axis' keyword in DataFrame.expanding is deprecated"
8889
expected = DataFrame(
8990
{i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)}
9091
)
9192
else:
9293
# axis == 1
94+
msg = "Support for axis=1 in DataFrame.expanding is deprecated"
9395
expected = DataFrame([[np.nan] * 2 + [float(i) for i in range(3, 21)]] * 10)
9496

95-
result = df.expanding(3, axis=axis_frame).sum()
97+
with tm.assert_produces_warning(FutureWarning, match=msg):
98+
result = df.expanding(3, axis=axis_frame).sum()
9699
tm.assert_frame_equal(result, expected)
97100

98101

@@ -323,7 +326,11 @@ def test_expanding_corr_pairwise(frame):
323326
)
324327
def test_expanding_func(func, static_comp, frame_or_series):
325328
data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10))
326-
result = getattr(data.expanding(min_periods=1, axis=0), func)()
329+
330+
msg = "The 'axis' keyword in (Series|DataFrame).expanding is deprecated"
331+
with tm.assert_produces_warning(FutureWarning, match=msg):
332+
obj = data.expanding(min_periods=1, axis=0)
333+
result = getattr(obj, func)()
327334
assert isinstance(result, frame_or_series)
328335

329336
expected = static_comp(data[:11])
@@ -341,26 +348,33 @@ def test_expanding_func(func, static_comp, frame_or_series):
341348
def test_expanding_min_periods(func, static_comp):
342349
ser = Series(np.random.randn(50))
343350

344-
result = getattr(ser.expanding(min_periods=30, axis=0), func)()
351+
msg = "The 'axis' keyword in Series.expanding is deprecated"
352+
with tm.assert_produces_warning(FutureWarning, match=msg):
353+
result = getattr(ser.expanding(min_periods=30, axis=0), func)()
345354
assert result[:29].isna().all()
346355
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
347356

348357
# min_periods is working correctly
349-
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
358+
with tm.assert_produces_warning(FutureWarning, match=msg):
359+
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
350360
assert isna(result.iloc[13])
351361
assert notna(result.iloc[14])
352362

353363
ser2 = Series(np.random.randn(20))
354-
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
364+
with tm.assert_produces_warning(FutureWarning, match=msg):
365+
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
355366
assert isna(result[3])
356367
assert notna(result[4])
357368

358369
# 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)()
370+
with tm.assert_produces_warning(FutureWarning, match=msg):
371+
result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
372+
with tm.assert_produces_warning(FutureWarning, match=msg):
373+
result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
361374
tm.assert_almost_equal(result0, result1)
362375

363-
result = getattr(ser.expanding(min_periods=1, axis=0), func)()
376+
with tm.assert_produces_warning(FutureWarning, match=msg):
377+
result = getattr(ser.expanding(min_periods=1, axis=0), func)()
364378
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
365379

366380

0 commit comments

Comments
 (0)