Skip to content

DEPR: DFGB.dtypes, rolling axis, Categorical.to_list #51997

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 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ Deprecations
- Deprecated :meth:`DataFrame._data` and :meth:`Series._data`, use public APIs instead (:issue:`33333`)
- 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`)
- 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`)
- Deprecated :meth:`DataFrameGroupBy.dtypes`, check ``dtypes`` on the underlying object instead (:issue:`51045`)
- 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.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`)
- 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`)
- 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`)
- 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`)
-
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,6 @@ def columns(self) -> Index:
def values(self):
return self.obj.values

@cache_readonly
def dtypes(self) -> Series:
return self.obj.dtypes

def apply(self) -> DataFrame | Series:
"""compute the results"""
# dispatch to agg
Expand Down
73 changes: 67 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11956,12 +11956,32 @@ 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)
name = "rolling"
if axis == 1:
warnings.warn(
f"Support for axis=1 in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
f"Use obj.T.{name}(...) instead",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
f"The 'axis' keyword in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
"Call the method without the axis keyword instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

if win_type is not None:
return Window(
Expand Down Expand Up @@ -11995,10 +12015,30 @@ 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)
name = "expanding"
if axis == 1:
warnings.warn(
f"Support for axis=1 in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
f"Use obj.T.{name}(...) instead",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
f"The 'axis' keyword in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
"Call the method without the axis keyword instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0
return Expanding(self, min_periods=min_periods, axis=axis, method=method)

@final
Expand All @@ -12012,11 +12052,32 @@ 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)
name = "ewm"
if axis == 1:
warnings.warn(
f"Support for axis=1 in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
f"Use obj.T.{name}(...) instead",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
warnings.warn(
f"The 'axis' keyword in {type(self).__name__}.{name} is "
"deprecated and will be removed in a future version. "
"Call the method without the axis keyword instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
axis = 0

return ExponentialMovingWindow(
self,
com=com,
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,14 @@ def hist(
@property
@doc(DataFrame.dtypes.__doc__)
def dtypes(self) -> Series:
# GH#51045
warnings.warn(
f"{type(self).__name__}.dtypes is deprecated and will be removed in "
"a future version. Check the dtypes on the base object instead",
FutureWarning,
stacklevel=find_stack_level(),
)

# error: Incompatible return value type (got "DataFrame", expected "Series")
return self.apply(lambda df: df.dtypes) # type: ignore[return-value]

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/groupby/test_allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def test_groupby_selection_other_methods(df):

# methods which aren't just .foo()
tm.assert_frame_equal(g.fillna(0), g_exp.fillna(0))
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
msg = "DataFrameGroupBy.dtypes is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
tm.assert_frame_equal(g.apply(lambda x: x.sum()), g_exp.apply(lambda x: x.sum()))

tm.assert_frame_equal(g.resample("D").mean(), g_exp.resample("D").mean())
Expand Down
27 changes: 14 additions & 13 deletions pandas/tests/window/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ 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)
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
with tm.assert_produces_warning(FutureWarning, match=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,29 +346,28 @@ 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()
msg = "The 'axis' keyword in Series.rolling is deprecated"
with tm.assert_produces_warning(FutureWarning, match=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()
df = DataFrame(np.ones((10, 10)))
msg = "The 'axis' keyword in DataFrame.rolling is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.rolling(window=3, center=True, axis=0, step=step).mean()
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.rolling(window=3, center=True, axis=1, step=step).mean()

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


def test_rolling_min_min_periods(step):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/window/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ 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)
msg = "Support for axis=1 in DataFrame.rolling is deprecated"
with tm.assert_produces_warning(FutureWarning, match=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)
4 changes: 3 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)
msg = "Support for axis=1 in DataFrame.ewm is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
e = df.ewm(alpha=0.5, axis=1)
result = getattr(e, func)()

tm.assert_frame_equal(result, expected)
Expand Down
30 changes: 22 additions & 8 deletions pandas/tests/window/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ def test_expanding_axis(axis_frame):
axis = df._get_axis_number(axis_frame)

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

result = df.expanding(3, axis=axis_frame).sum()
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.expanding(3, axis=axis_frame).sum()
tm.assert_frame_equal(result, expected)


Expand Down Expand Up @@ -323,7 +326,11 @@ def test_expanding_corr_pairwise(frame):
)
def test_expanding_func(func, static_comp, frame_or_series):
data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10))
result = getattr(data.expanding(min_periods=1, axis=0), func)()

msg = "The 'axis' keyword in (Series|DataFrame).expanding is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
obj = data.expanding(min_periods=1, axis=0)
result = getattr(obj, func)()
assert isinstance(result, frame_or_series)

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

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

# min_periods is working correctly
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
with tm.assert_produces_warning(FutureWarning, match=msg):
result = getattr(ser.expanding(min_periods=15, axis=0), func)()
assert isna(result.iloc[13])
assert notna(result.iloc[14])

ser2 = Series(np.random.randn(20))
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
with tm.assert_produces_warning(FutureWarning, match=msg):
result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
assert isna(result[3])
assert notna(result[4])

# min_periods=0
result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
with tm.assert_produces_warning(FutureWarning, match=msg):
result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
with tm.assert_produces_warning(FutureWarning, match=msg):
result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
tm.assert_almost_equal(result0, result1)

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


Expand Down
Loading