diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index affef80571fce..8568230dd07b8 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1412,6 +1412,7 @@ Timezones - Bug in :class:`Timestamp` constructor where a ``dateutil.tz.tzutc`` timezone passed with a ``datetime.datetime`` argument would be converted to a ``pytz.UTC`` timezone (:issue:`23807`) - Bug in :func:`to_datetime` where ``utc=True`` was not respected when specifying a ``unit`` and ``errors='ignore'`` (:issue:`23758`) - Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`) +- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`) Offsets ^^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a34a34186cf45..99ae551d3c55b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7291,7 +7291,7 @@ def f(x): if filter_type is None or filter_type == 'numeric': data = self._get_numeric_data() elif filter_type == 'bool': - data = self._get_bool_data() + data = self else: # pragma: no cover msg = ("Generating numeric_only data with filter_type {f}" "not supported.".format(f=filter_type)) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 6f68828b94a84..73ea2c679913c 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -228,13 +228,6 @@ def assert_bool_op_api(opname, bool_frame_with_na, float_string_frame, getattr(mixed, opname)(axis=0) getattr(mixed, opname)(axis=1) - class NonzeroFail(object): - - def __nonzero__(self): - raise ValueError - - mixed['_nonzero_fail_'] = NonzeroFail() - if has_bool_only: getattr(mixed, opname)(axis=0, bool_only=True) getattr(mixed, opname)(axis=1, bool_only=True) @@ -1374,25 +1367,22 @@ def test_any_all_extra(self): result = df[['C']].all(axis=None).item() assert result is True - # skip pathological failure cases - # class CantNonzero(object): - - # def __nonzero__(self): - # raise ValueError + def test_any_datetime(self): - # df[4] = CantNonzero() - - # it works! - # df.any(1) - # df.all(1) - # df.any(1, bool_only=True) - # df.all(1, bool_only=True) + # GH 23070 + float_data = [1, np.nan, 3, np.nan] + datetime_data = [pd.Timestamp('1960-02-15'), + pd.Timestamp('1960-02-16'), + pd.NaT, + pd.NaT] + df = DataFrame({ + "A": float_data, + "B": datetime_data + }) - # df[4][4] = np.nan - # df.any(1) - # df.all(1) - # df.any(1, bool_only=True) - # df.all(1, bool_only=True) + result = df.any(1) + expected = Series([True, True, True, False]) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize('func, data, expected', [ (np.any, {}, False),