Skip to content

CLN: enforce any/all deprecation with datetime64 #58029

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 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Removal of prior version deprecations/changes
- All arguments in :meth:`Series.to_dict` are now keyword only (:issue:`56493`)
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
- 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`)
- Enforced deprecation ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes (:issue:`58029`)
- Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`)
- 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`)
- 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`)
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,12 +1665,7 @@ def _groupby_op(
raise TypeError(f"datetime64 type does not support {how} operations")
if how in ["any", "all"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you combine this if with the one above?

Copy link
Contributor Author

@natmokval natmokval Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure. Done

# GH#34479
warnings.warn(
f"'{how}' with datetime64 dtypes is deprecated and will raise in a "
f"future version. Use (obj != pd.Timestamp(0)).{how}() instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
raise TypeError(f"'{how}' with datetime64 dtypes is not supported")

elif isinstance(dtype, PeriodDtype):
# Adding/multiplying Periods is not valid
Expand Down Expand Up @@ -2217,11 +2212,11 @@ def ceil(
# Reductions

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

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

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

Expand Down
15 changes: 2 additions & 13 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
npt,
)
from pandas.compat._optional import import_optional_dependency
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_complex,
Expand Down Expand Up @@ -521,12 +520,7 @@ def nanany(

if values.dtype.kind == "M":
# GH#34479
warnings.warn(
"'any' with datetime64 dtypes is deprecated and will raise in a "
"future version. Use (obj != pd.Timestamp(0)).any() instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
raise TypeError("'any' with datetime64 dtypes is not supported")

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

Expand Down Expand Up @@ -582,12 +576,7 @@ def nanall(

if values.dtype.kind == "M":
# GH#34479
warnings.warn(
"'all' with datetime64 dtypes is deprecated and will raise in a "
"future version. Use (obj != pd.Timestamp(0)).all() instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
raise TypeError("'all' with datetime64 dtypes is not supported")

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

Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/extension/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ def _supports_reduction(self, obj, op_name: str) -> bool:
@pytest.mark.parametrize("skipna", [True, False])
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
meth = all_boolean_reductions
msg = f"'{meth}' with datetime64 dtypes is deprecated and will raise in"
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
msg = f"'{meth}' with datetime64 dtypes is not supported"
with pytest.raises(TypeError, match=msg):
super().test_reduce_series_boolean(data, all_boolean_reductions, skipna)

def test_series_constructor(self, data):
Expand Down
35 changes: 15 additions & 20 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,6 @@ def test_any_all_object_dtype(
expected = Series([True, True, val, True])
tm.assert_series_equal(result, expected)

# GH#50947 deprecates this but it is not emitting a warning in some builds.
@pytest.mark.filterwarnings(
"ignore:'any' with datetime64 dtypes is deprecated.*:FutureWarning"
)
def test_any_datetime(self):
# GH 23070
float_data = [1, np.nan, 3, np.nan]
Expand All @@ -1386,10 +1382,9 @@ def test_any_datetime(self):
]
df = DataFrame({"A": float_data, "B": datetime_data})

result = df.any(axis=1)

expected = Series([True, True, True, False])
tm.assert_series_equal(result, expected)
msg = "'any' with datetime64 dtypes is not supported"
with pytest.raises(TypeError, match=msg):
df.any(axis=1)

def test_any_all_bool_only(self):
# GH 25101
Expand Down Expand Up @@ -1481,23 +1476,23 @@ def test_any_all_np_func(self, func, data, expected):
TypeError, match="dtype category does not support reduction"
):
getattr(DataFrame(data), func.__name__)(axis=None)
else:
msg = "'(any|all)' with datetime64 dtypes is deprecated"
if data.dtypes.apply(lambda x: x.kind == "M").any():
warn = FutureWarning
else:
warn = None
if data.dtypes.apply(lambda x: x.kind == "M").any():
# GH#34479
msg = "'(any|all)' with datetime64 dtypes is not supported"
with pytest.raises(TypeError, match=msg):
func(data)

# method version
with pytest.raises(TypeError, match=msg):
getattr(DataFrame(data), func.__name__)(axis=None)

with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
# GH#34479
result = func(data)
elif data.dtypes.apply(lambda x: x != "category").any():
result = func(data)
assert isinstance(result, np.bool_)
assert result.item() is expected

# method version
with tm.assert_produces_warning(warn, match=msg):
# GH#34479
result = getattr(DataFrame(data), func.__name__)(axis=None)
result = getattr(DataFrame(data), func.__name__)(axis=None)
assert isinstance(result, np.bool_)
assert result.item() is expected

Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/groupby/test_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ def test_groupby_raises_datetime(
return

klass, msg = {
"all": (None, ""),
"any": (None, ""),
"all": (TypeError, "'all' with datetime64 dtypes is not supported"),
"any": (TypeError, "'any' with datetime64 dtypes is not supported"),
"bfill": (None, ""),
"corrwith": (TypeError, "cannot perform __mul__ with this index type"),
"count": (None, ""),
Expand Down Expand Up @@ -285,9 +285,7 @@ def test_groupby_raises_datetime(
"var": (TypeError, "datetime64 type does not support var operations"),
}[groupby_func]

if groupby_func in ["any", "all"]:
warn_msg = f"'{groupby_func}' with datetime64 dtypes is deprecated"
elif groupby_func == "fillna":
if groupby_func == "fillna":
kind = "Series" if groupby_series else "DataFrame"
warn_msg = f"{kind}GroupBy.fillna is deprecated"
else:
Expand Down
45 changes: 27 additions & 18 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,32 +1009,41 @@ def test_any_all_datetimelike(self):
ser = Series(dta)
df = DataFrame(ser)

msg = "'(any|all)' with datetime64 dtypes is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
# GH#34479
assert dta.all()
assert dta.any()
# GH#34479
msg = "'(any|all)' with datetime64 dtypes is not supported"
with pytest.raises(TypeError, match=msg):
dta.all()
with pytest.raises(TypeError, match=msg):
dta.any()

assert ser.all()
assert ser.any()
with pytest.raises(TypeError, match=msg):
ser.all()
with pytest.raises(TypeError, match=msg):
ser.any()

assert df.any().all()
assert df.all().all()
with pytest.raises(TypeError, match=msg):
df.any().all()
with pytest.raises(TypeError, match=msg):
df.all().all()

dta = dta.tz_localize("UTC")
ser = Series(dta)
df = DataFrame(ser)
# GH#34479
with pytest.raises(TypeError, match=msg):
dta.all()
with pytest.raises(TypeError, match=msg):
dta.any()

with tm.assert_produces_warning(FutureWarning, match=msg):
# GH#34479
assert dta.all()
assert dta.any()

assert ser.all()
assert ser.any()
with pytest.raises(TypeError, match=msg):
ser.all()
with pytest.raises(TypeError, match=msg):
ser.any()

assert df.any().all()
assert df.all().all()
with pytest.raises(TypeError, match=msg):
df.any().all()
with pytest.raises(TypeError, match=msg):
df.all().all()

tda = dta - dta[0]
ser = Series(tda)
Expand Down