Skip to content

Commit a5d1bb4

Browse files
authored
DEPR: dt64 any/all GH#34479 (#50947)
* DEPR: dt64 any/all GH#34479 * GH ref * troubleshoot * filter warning
1 parent 365841f commit a5d1bb4

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ Deprecations
639639
- :meth:`Index.is_categorical` has been deprecated. Use :func:`pandas.api.types.is_categorical_dtype` instead (:issue:`50042`)
640640
- :meth:`Index.is_object` has been deprecated. Use :func:`pandas.api.types.is_object_dtype` instead (:issue:`50042`)
641641
- :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`)
642+
- Deprecated ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes, use e.g. ``(obj != pd.Timestamp(0), tz=obj.tz).all()`` instead (:issue:`34479`)
642643
-
643644

644645
.. ---------------------------------------------------------------------------

pandas/core/arrays/datetimelike.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2034,11 +2034,12 @@ def ceil(
20342034
# Reductions
20352035

20362036
def any(self, *, axis: AxisInt | None = None, skipna: bool = True) -> bool:
2037-
# GH#34479 discussion of desired behavior long-term
2037+
# GH#34479 the nanops call will issue a FutureWarning for non-td64 dtype
20382038
return nanops.nanany(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
20392039

20402040
def all(self, *, axis: AxisInt | None = None, skipna: bool = True) -> bool:
2041-
# GH#34479 discussion of desired behavior long-term
2041+
# GH#34479 the nanops call will issue a FutureWarning for non-td64 dtype
2042+
20422043
return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())
20432044

20442045
# --------------------------------------------------------------

pandas/core/nanops.py

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
npt,
3333
)
3434
from pandas.compat._optional import import_optional_dependency
35+
from pandas.util._exceptions import find_stack_level
3536

3637
from pandas.core.dtypes.common import (
3738
is_any_int_dtype,
@@ -529,6 +530,15 @@ def nanany(
529530
>>> nanops.nanany(s)
530531
False
531532
"""
533+
if needs_i8_conversion(values.dtype) and values.dtype.kind != "m":
534+
# GH#34479
535+
warnings.warn(
536+
"'any' with datetime64 dtypes is deprecated and will raise in a "
537+
"future version. Use (obj != pd.Timestamp(0)).any() instead.",
538+
FutureWarning,
539+
stacklevel=find_stack_level(),
540+
)
541+
532542
values, _, _, _, _ = _get_values(values, skipna, fill_value=False, mask=mask)
533543

534544
# For object type, any won't necessarily return
@@ -575,6 +585,15 @@ def nanall(
575585
>>> nanops.nanall(s)
576586
False
577587
"""
588+
if needs_i8_conversion(values.dtype) and values.dtype.kind != "m":
589+
# GH#34479
590+
warnings.warn(
591+
"'all' with datetime64 dtypes is deprecated and will raise in a "
592+
"future version. Use (obj != pd.Timestamp(0)).all() instead.",
593+
FutureWarning,
594+
stacklevel=find_stack_level(),
595+
)
596+
578597
values, _, _, _, _ = _get_values(values, skipna, fill_value=True, mask=mask)
579598

580599
# For object type, all won't necessarily return

pandas/tests/frame/test_reductions.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,10 @@ def test_any_all_object_dtype(self, axis, bool_agg_func, skipna):
11381138
expected = Series([True, True, True, True])
11391139
tm.assert_series_equal(result, expected)
11401140

1141+
# GH#50947 deprecates this but it is not emitting a warning in some builds.
1142+
@pytest.mark.filterwarnings(
1143+
"ignore:'any' with datetime64 dtypes is deprecated.*:FutureWarning"
1144+
)
11411145
def test_any_datetime(self):
11421146

11431147
# GH 23070
@@ -1151,6 +1155,7 @@ def test_any_datetime(self):
11511155
df = DataFrame({"A": float_data, "B": datetime_data})
11521156

11531157
result = df.any(axis=1)
1158+
11541159
expected = Series([True, True, True, False])
11551160
tm.assert_series_equal(result, expected)
11561161

@@ -1245,12 +1250,22 @@ def test_any_all_np_func(self, func, data, expected):
12451250
):
12461251
getattr(DataFrame(data), func.__name__)(axis=None)
12471252
else:
1248-
result = func(data)
1253+
msg = "'(any|all)' with datetime64 dtypes is deprecated"
1254+
if data.dtypes.apply(lambda x: x.kind == "M").any():
1255+
warn = FutureWarning
1256+
else:
1257+
warn = None
1258+
1259+
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
1260+
# GH#34479
1261+
result = func(data)
12491262
assert isinstance(result, np.bool_)
12501263
assert result.item() is expected
12511264

12521265
# method version
1253-
result = getattr(DataFrame(data), func.__name__)(axis=None)
1266+
with tm.assert_produces_warning(warn, match=msg):
1267+
# GH#34479
1268+
result = getattr(DataFrame(data), func.__name__)(axis=None)
12541269
assert isinstance(result, np.bool_)
12551270
assert result.item() is expected
12561271

pandas/tests/reductions/test_reductions.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -985,27 +985,32 @@ def test_any_all_datetimelike(self):
985985
ser = Series(dta)
986986
df = DataFrame(ser)
987987

988-
assert dta.all()
989-
assert dta.any()
988+
msg = "'(any|all)' with datetime64 dtypes is deprecated"
989+
with tm.assert_produces_warning(FutureWarning, match=msg):
990+
# GH#34479
991+
assert dta.all()
992+
assert dta.any()
990993

991-
assert ser.all()
992-
assert ser.any()
994+
assert ser.all()
995+
assert ser.any()
993996

994-
assert df.any().all()
995-
assert df.all().all()
997+
assert df.any().all()
998+
assert df.all().all()
996999

9971000
dta = dta.tz_localize("UTC")
9981001
ser = Series(dta)
9991002
df = DataFrame(ser)
10001003

1001-
assert dta.all()
1002-
assert dta.any()
1004+
with tm.assert_produces_warning(FutureWarning, match=msg):
1005+
# GH#34479
1006+
assert dta.all()
1007+
assert dta.any()
10031008

1004-
assert ser.all()
1005-
assert ser.any()
1009+
assert ser.all()
1010+
assert ser.any()
10061011

1007-
assert df.any().all()
1008-
assert df.all().all()
1012+
assert df.any().all()
1013+
assert df.all().all()
10091014

10101015
tda = dta - dta[0]
10111016
ser = Series(tda)

0 commit comments

Comments
 (0)