Skip to content

Commit 4088ec2

Browse files
authored
CLN: Enforce deprecation of DataFrame.idxmin/max with EAs and axis=1 (#62201)
1 parent 5b6140c commit 4088ec2

File tree

5 files changed

+21
-44
lines changed

5 files changed

+21
-44
lines changed

pandas/core/frame.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
)
7272
from pandas.util._exceptions import (
7373
find_stack_level,
74-
rewrite_warning,
7574
)
7675
from pandas.util._validators import (
7776
validate_ascending,
@@ -11926,25 +11925,13 @@ def _get_data() -> DataFrame:
1192611925
row_index = np.tile(np.arange(nrows), ncols)
1192711926
col_index = np.repeat(np.arange(ncols), nrows)
1192811927
ser = Series(arr, index=col_index, copy=False)
11929-
# GroupBy will raise a warning with SeriesGroupBy as the object,
11930-
# likely confusing users
11931-
with rewrite_warning(
11932-
target_message=(
11933-
f"The behavior of SeriesGroupBy.{name} with all-NA values"
11934-
),
11935-
target_category=FutureWarning,
11936-
new_message=(
11937-
f"The behavior of {type(self).__name__}.{name} with all-NA "
11938-
"values, or any-NA and skipna=False, is deprecated. In "
11939-
"a future version this will raise ValueError"
11940-
),
11941-
):
11942-
result = ser.groupby(row_index).agg(name, **kwds)
11928+
if name == "all":
11929+
# Behavior here appears incorrect; preserving
11930+
# for backwards compatibility for now.
11931+
# See https://github.com/pandas-dev/pandas/issues/57171
11932+
skipna = True
11933+
result = ser.groupby(row_index).agg(name, **kwds, skipna=skipna)
1194311934
result.index = df.index
11944-
if not skipna and name not in ("any", "all"):
11945-
mask = df.isna().to_numpy(dtype=np.bool_).any(axis=1)
11946-
other = -1 if name in ("idxmax", "idxmin") else lib.no_default
11947-
result = result.mask(mask, other)
1194811935
return result
1194911936

1195011937
df = df.T
@@ -13258,13 +13245,11 @@ def idxmin(
1325813245
# indices will always be np.ndarray since axis is not N
1325913246

1326013247
if (indices == -1).any():
13261-
warnings.warn(
13262-
f"The behavior of {type(self).__name__}.idxmin with all-NA "
13263-
"values, or any-NA and skipna=False, is deprecated. In a future "
13264-
"version this will raise ValueError",
13265-
FutureWarning,
13266-
stacklevel=find_stack_level(),
13267-
)
13248+
if skipna:
13249+
msg = "Encountered all NA values"
13250+
else:
13251+
msg = "Encountered an NA values with skipna=False"
13252+
raise ValueError(msg)
1326813253

1326913254
index = data._get_axis(axis)
1327013255
result = algorithms.take(
@@ -13365,13 +13350,11 @@ def idxmax(
1336513350
# indices will always be 1d array since axis is not None
1336613351

1336713352
if (indices == -1).any():
13368-
warnings.warn(
13369-
f"The behavior of {type(self).__name__}.idxmax with all-NA "
13370-
"values, or any-NA and skipna=False, is deprecated. In a future "
13371-
"version this will raise ValueError",
13372-
FutureWarning,
13373-
stacklevel=find_stack_level(),
13374-
)
13353+
if skipna:
13354+
msg = "Encountered all NA values"
13355+
else:
13356+
msg = "Encountered an NA values with skipna=False"
13357+
raise ValueError(msg)
1337513358

1337613359
index = data._get_axis(axis)
1337713360
result = algorithms.take(

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,10 +5703,7 @@ def _idxmax_idxmin(
57035703
"Specify observed=True in groupby instead."
57045704
)
57055705
elif not skipna and self._obj_with_exclusions.isna().any(axis=None):
5706-
raise ValueError(
5707-
f"{type(self).__name__}.{how} with skipna=False encountered an NA "
5708-
f"value."
5709-
)
5706+
raise ValueError(f"{how} with skipna=False encountered an NA value.")
57105707

57115708
result = self._agg_general(
57125709
numeric_only=numeric_only,
@@ -5724,8 +5721,7 @@ def _wrap_idxmax_idxmin(
57245721
result = res.astype(index.dtype)
57255722
elif skipna and res.lt(0).any(axis=None):
57265723
raise ValueError(
5727-
f"{type(self).__name__}.{how} with skipna=True encountered all NA "
5728-
f"values in a group."
5724+
f"{how} with skipna=True encountered all NA values in a group."
57295725
)
57305726
else:
57315727
if isinstance(index, MultiIndex):

pandas/tests/frame/test_reductions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,9 +2160,7 @@ def test_numeric_ea_axis_1(method, skipna, min_count, any_numeric_ea_dtype):
21602160
kwargs["min_count"] = min_count
21612161

21622162
if not skipna and method in ("idxmax", "idxmin"):
2163-
# GH#57745 - EAs use groupby for axis=1 which still needs a proper deprecation.
2164-
msg = f"The behavior of DataFrame.{method} with all-NA values"
2165-
with tm.assert_produces_warning(FutureWarning, match=msg):
2163+
with pytest.raises(ValueError, match="encountered an NA value"):
21662164
getattr(df, method)(axis=1, **kwargs)
21672165
with pytest.raises(ValueError, match="Encountered an NA value"):
21682166
getattr(expected_df, method)(axis=1, **kwargs)

pandas/tests/groupby/test_reductions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def test_idxmin_idxmax_extremes_skipna(skipna, how, float_numpy_dtype):
291291
gb = df.groupby("a")
292292

293293
if not skipna:
294-
msg = f"DataFrameGroupBy.{how} with skipna=False"
294+
msg = f"{how} with skipna=False"
295295
with pytest.raises(ValueError, match=msg):
296296
getattr(gb, how)(skipna=skipna)
297297
return

pandas/tests/groupby/transform/test_transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ def test_idxmin_idxmax_transform_args(how, skipna, numeric_only):
14941494
expected = gb.transform(how, skipna=skipna, numeric_only=numeric_only)
14951495
tm.assert_frame_equal(result, expected)
14961496
else:
1497-
msg = f"DataFrameGroupBy.{how} with skipna=False encountered an NA value"
1497+
msg = f"{how} with skipna=False encountered an NA value"
14981498
with pytest.raises(ValueError, match=msg):
14991499
gb.transform(how, skipna, numeric_only)
15001500

0 commit comments

Comments
 (0)