Skip to content

Commit 115cbe1

Browse files
natmokvalpmhatre1
authored andcommitted
CLN: enforce any/all deprecation with datetime64 (pandas-dev#58029)
* enforce deprecation any/all with datetime64, correct tests * add a note to v3.0.0 * enforce depr any/all with dt64 in groupby * combine two if * change wording in the error msg * fix test_in_numeric_groupby * fix test_cython_transform_frame_column
1 parent 03f084e commit 115cbe1

File tree

11 files changed

+68
-81
lines changed

11 files changed

+68
-81
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Removal of prior version deprecations/changes
214214
- All arguments in :meth:`Series.to_dict` are now keyword only (:issue:`56493`)
215215
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
216216
- Enforce deprecation in :func:`testing.assert_series_equal` and :func:`testing.assert_frame_equal` with object dtype and mismatched null-like values, which are now considered not-equal (:issue:`18463`)
217+
- Enforced deprecation ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes (:issue:`58029`)
217218
- Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`)
218219
- Enforced deprecation in :meth:`Series.value_counts` and :meth:`Index.value_counts` with object dtype performing dtype inference on the ``.index`` of the result (:issue:`56161`)
219220
- Enforced deprecation of :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` allowing the ``name`` argument to be a non-tuple when grouping by a list of length 1 (:issue:`54155`)

pandas/core/arrays/datetimelike.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -1661,16 +1661,8 @@ def _groupby_op(
16611661
dtype = self.dtype
16621662
if dtype.kind == "M":
16631663
# Adding/multiplying datetimes is not valid
1664-
if how in ["sum", "prod", "cumsum", "cumprod", "var", "skew"]:
1665-
raise TypeError(f"datetime64 type does not support {how} operations")
1666-
if how in ["any", "all"]:
1667-
# GH#34479
1668-
warnings.warn(
1669-
f"'{how}' with datetime64 dtypes is deprecated and will raise in a "
1670-
f"future version. Use (obj != pd.Timestamp(0)).{how}() instead.",
1671-
FutureWarning,
1672-
stacklevel=find_stack_level(),
1673-
)
1664+
if how in ["any", "all", "sum", "prod", "cumsum", "cumprod", "var", "skew"]:
1665+
raise TypeError(f"datetime64 type does not support operation: '{how}'")
16741666

16751667
elif isinstance(dtype, PeriodDtype):
16761668
# Adding/multiplying Periods is not valid
@@ -2217,11 +2209,11 @@ def ceil(
22172209
# Reductions
22182210

22192211
def any(self, *, axis: AxisInt | None = None, skipna: bool = True) -> bool:
2220-
# GH#34479 the nanops call will issue a FutureWarning for non-td64 dtype
2212+
# GH#34479 the nanops call will raise a TypeError for non-td64 dtype
22212213
return nanops.nanany(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
22222214

22232215
def all(self, *, axis: AxisInt | None = None, skipna: bool = True) -> bool:
2224-
# GH#34479 the nanops call will issue a FutureWarning for non-td64 dtype
2216+
# GH#34479 the nanops call will raise a TypeError for non-td64 dtype
22252217

22262218
return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
22272219

pandas/core/nanops.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
npt,
3232
)
3333
from pandas.compat._optional import import_optional_dependency
34-
from pandas.util._exceptions import find_stack_level
3534

3635
from pandas.core.dtypes.common import (
3736
is_complex,
@@ -521,12 +520,7 @@ def nanany(
521520

522521
if values.dtype.kind == "M":
523522
# GH#34479
524-
warnings.warn(
525-
"'any' with datetime64 dtypes is deprecated and will raise in a "
526-
"future version. Use (obj != pd.Timestamp(0)).any() instead.",
527-
FutureWarning,
528-
stacklevel=find_stack_level(),
529-
)
523+
raise TypeError("datetime64 type does not support operation: 'any'")
530524

531525
values, _ = _get_values(values, skipna, fill_value=False, mask=mask)
532526

@@ -582,12 +576,7 @@ def nanall(
582576

583577
if values.dtype.kind == "M":
584578
# GH#34479
585-
warnings.warn(
586-
"'all' with datetime64 dtypes is deprecated and will raise in a "
587-
"future version. Use (obj != pd.Timestamp(0)).all() instead.",
588-
FutureWarning,
589-
stacklevel=find_stack_level(),
590-
)
579+
raise TypeError("datetime64 type does not support operation: 'all'")
591580

592581
values, _ = _get_values(values, skipna, fill_value=True, mask=mask)
593582

pandas/tests/extension/base/groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ def test_in_numeric_groupby(self, data_for_grouping):
162162

163163
msg = "|".join(
164164
[
165-
# period/datetime
165+
# period
166166
"does not support sum operations",
167+
# datetime
168+
"does not support operation: 'sum'",
167169
# all others
168170
re.escape(f"agg function failed [how->sum,dtype->{dtype}"),
169171
]

pandas/tests/extension/test_datetime.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ def _supports_reduction(self, obj, op_name: str) -> bool:
104104
@pytest.mark.parametrize("skipna", [True, False])
105105
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
106106
meth = all_boolean_reductions
107-
msg = f"'{meth}' with datetime64 dtypes is deprecated and will raise in"
108-
with tm.assert_produces_warning(
109-
FutureWarning, match=msg, check_stacklevel=False
110-
):
107+
msg = f"datetime64 type does not support operation: '{meth}'"
108+
with pytest.raises(TypeError, match=msg):
111109
super().test_reduce_series_boolean(data, all_boolean_reductions, skipna)
112110

113111
def test_series_constructor(self, data):

pandas/tests/frame/test_reductions.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,6 @@ def test_any_all_object_dtype(
13701370
expected = Series([True, True, val, True])
13711371
tm.assert_series_equal(result, expected)
13721372

1373-
# GH#50947 deprecates this but it is not emitting a warning in some builds.
1374-
@pytest.mark.filterwarnings(
1375-
"ignore:'any' with datetime64 dtypes is deprecated.*:FutureWarning"
1376-
)
13771373
def test_any_datetime(self):
13781374
# GH 23070
13791375
float_data = [1, np.nan, 3, np.nan]
@@ -1385,10 +1381,9 @@ def test_any_datetime(self):
13851381
]
13861382
df = DataFrame({"A": float_data, "B": datetime_data})
13871383

1388-
result = df.any(axis=1)
1389-
1390-
expected = Series([True, True, True, False])
1391-
tm.assert_series_equal(result, expected)
1384+
msg = "datetime64 type does not support operation: 'any'"
1385+
with pytest.raises(TypeError, match=msg):
1386+
df.any(axis=1)
13921387

13931388
def test_any_all_bool_only(self):
13941389
# GH 25101
@@ -1480,23 +1475,23 @@ def test_any_all_np_func(self, func, data, expected):
14801475
TypeError, match="dtype category does not support reduction"
14811476
):
14821477
getattr(DataFrame(data), func.__name__)(axis=None)
1483-
else:
1484-
msg = "'(any|all)' with datetime64 dtypes is deprecated"
1485-
if data.dtypes.apply(lambda x: x.kind == "M").any():
1486-
warn = FutureWarning
1487-
else:
1488-
warn = None
1478+
if data.dtypes.apply(lambda x: x.kind == "M").any():
1479+
# GH#34479
1480+
msg = "datetime64 type does not support operation: '(any|all)'"
1481+
with pytest.raises(TypeError, match=msg):
1482+
func(data)
1483+
1484+
# method version
1485+
with pytest.raises(TypeError, match=msg):
1486+
getattr(DataFrame(data), func.__name__)(axis=None)
14891487

1490-
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
1491-
# GH#34479
1492-
result = func(data)
1488+
elif data.dtypes.apply(lambda x: x != "category").any():
1489+
result = func(data)
14931490
assert isinstance(result, np.bool_)
14941491
assert result.item() is expected
14951492

14961493
# method version
1497-
with tm.assert_produces_warning(warn, match=msg):
1498-
# GH#34479
1499-
result = getattr(DataFrame(data), func.__name__)(axis=None)
1494+
result = getattr(DataFrame(data), func.__name__)(axis=None)
15001495
assert isinstance(result, np.bool_)
15011496
assert result.item() is expected
15021497

pandas/tests/groupby/test_groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def test_raises_on_nuisance(df):
674674
df = df.loc[:, ["A", "C", "D"]]
675675
df["E"] = datetime.now()
676676
grouped = df.groupby("A")
677-
msg = "datetime64 type does not support sum operations"
677+
msg = "datetime64 type does not support operation: 'sum'"
678678
with pytest.raises(TypeError, match=msg):
679679
grouped.agg("sum")
680680
with pytest.raises(TypeError, match=msg):

pandas/tests/groupby/test_raises.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ def test_groupby_raises_datetime(
241241
return
242242

243243
klass, msg = {
244-
"all": (None, ""),
245-
"any": (None, ""),
244+
"all": (TypeError, "datetime64 type does not support operation: 'all'"),
245+
"any": (TypeError, "datetime64 type does not support operation: 'any'"),
246246
"bfill": (None, ""),
247247
"corrwith": (TypeError, "cannot perform __mul__ with this index type"),
248248
"count": (None, ""),
249249
"cumcount": (None, ""),
250250
"cummax": (None, ""),
251251
"cummin": (None, ""),
252-
"cumprod": (TypeError, "datetime64 type does not support cumprod operations"),
253-
"cumsum": (TypeError, "datetime64 type does not support cumsum operations"),
252+
"cumprod": (TypeError, "datetime64 type does not support operation: 'cumprod'"),
253+
"cumsum": (TypeError, "datetime64 type does not support operation: 'cumsum'"),
254254
"diff": (None, ""),
255255
"ffill": (None, ""),
256256
"fillna": (None, ""),
@@ -265,7 +265,7 @@ def test_groupby_raises_datetime(
265265
"ngroup": (None, ""),
266266
"nunique": (None, ""),
267267
"pct_change": (TypeError, "cannot perform __truediv__ with this index type"),
268-
"prod": (TypeError, "datetime64 type does not support prod"),
268+
"prod": (TypeError, "datetime64 type does not support operation: 'prod'"),
269269
"quantile": (None, ""),
270270
"rank": (None, ""),
271271
"sem": (None, ""),
@@ -276,18 +276,16 @@ def test_groupby_raises_datetime(
276276
"|".join(
277277
[
278278
r"dtype datetime64\[ns\] does not support reduction",
279-
"datetime64 type does not support skew operations",
279+
"datetime64 type does not support operation: 'skew'",
280280
]
281281
),
282282
),
283283
"std": (None, ""),
284-
"sum": (TypeError, "datetime64 type does not support sum operations"),
285-
"var": (TypeError, "datetime64 type does not support var operations"),
284+
"sum": (TypeError, "datetime64 type does not support operation: 'sum"),
285+
"var": (TypeError, "datetime64 type does not support operation: 'var'"),
286286
}[groupby_func]
287287

288-
if groupby_func in ["any", "all"]:
289-
warn_msg = f"'{groupby_func}' with datetime64 dtypes is deprecated"
290-
elif groupby_func == "fillna":
288+
if groupby_func == "fillna":
291289
kind = "Series" if groupby_series else "DataFrame"
292290
warn_msg = f"{kind}GroupBy.fillna is deprecated"
293291
else:

pandas/tests/groupby/transform/test_transform.py

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ def test_cython_transform_frame_column(
749749
msg = "|".join(
750750
[
751751
"does not support .* operations",
752+
"does not support operation",
752753
".* is not supported for object dtype",
753754
"is not implemented for this dtype",
754755
]

pandas/tests/reductions/test_reductions.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -1009,32 +1009,41 @@ def test_any_all_datetimelike(self):
10091009
ser = Series(dta)
10101010
df = DataFrame(ser)
10111011

1012-
msg = "'(any|all)' with datetime64 dtypes is deprecated"
1013-
with tm.assert_produces_warning(FutureWarning, match=msg):
1014-
# GH#34479
1015-
assert dta.all()
1016-
assert dta.any()
1012+
# GH#34479
1013+
msg = "datetime64 type does not support operation: '(any|all)'"
1014+
with pytest.raises(TypeError, match=msg):
1015+
dta.all()
1016+
with pytest.raises(TypeError, match=msg):
1017+
dta.any()
10171018

1018-
assert ser.all()
1019-
assert ser.any()
1019+
with pytest.raises(TypeError, match=msg):
1020+
ser.all()
1021+
with pytest.raises(TypeError, match=msg):
1022+
ser.any()
10201023

1021-
assert df.any().all()
1022-
assert df.all().all()
1024+
with pytest.raises(TypeError, match=msg):
1025+
df.any().all()
1026+
with pytest.raises(TypeError, match=msg):
1027+
df.all().all()
10231028

10241029
dta = dta.tz_localize("UTC")
10251030
ser = Series(dta)
10261031
df = DataFrame(ser)
1032+
# GH#34479
1033+
with pytest.raises(TypeError, match=msg):
1034+
dta.all()
1035+
with pytest.raises(TypeError, match=msg):
1036+
dta.any()
10271037

1028-
with tm.assert_produces_warning(FutureWarning, match=msg):
1029-
# GH#34479
1030-
assert dta.all()
1031-
assert dta.any()
1032-
1033-
assert ser.all()
1034-
assert ser.any()
1038+
with pytest.raises(TypeError, match=msg):
1039+
ser.all()
1040+
with pytest.raises(TypeError, match=msg):
1041+
ser.any()
10351042

1036-
assert df.any().all()
1037-
assert df.all().all()
1043+
with pytest.raises(TypeError, match=msg):
1044+
df.any().all()
1045+
with pytest.raises(TypeError, match=msg):
1046+
df.all().all()
10381047

10391048
tda = dta - dta[0]
10401049
ser = Series(tda)

pandas/tests/resample/test_resample_api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,9 @@ def test_selection_api_validation():
708708
tm.assert_frame_equal(exp, result)
709709

710710
exp.index.name = "d"
711-
with pytest.raises(TypeError, match="datetime64 type does not support sum"):
711+
with pytest.raises(
712+
TypeError, match="datetime64 type does not support operation: 'sum'"
713+
):
712714
df.resample("2D", level="d").sum()
713715
result = df.resample("2D", level="d").sum(numeric_only=True)
714716
tm.assert_frame_equal(exp, result)

0 commit comments

Comments
 (0)