Skip to content

Commit 36ab8c9

Browse files
makbigcjreback
authored andcommitted
BUG: Pandas any() returning false with true values present (GH #23070) (#24434)
1 parent 460945e commit 36ab8c9

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,7 @@ Timezones
14131413
- 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`)
14141414
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when specifying a ``unit`` and ``errors='ignore'`` (:issue:`23758`)
14151415
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)
1416+
- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`)
14161417

14171418
Offsets
14181419
^^^^^^^

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7291,7 +7291,7 @@ def f(x):
72917291
if filter_type is None or filter_type == 'numeric':
72927292
data = self._get_numeric_data()
72937293
elif filter_type == 'bool':
7294-
data = self._get_bool_data()
7294+
data = self
72957295
else: # pragma: no cover
72967296
msg = ("Generating numeric_only data with filter_type {f}"
72977297
"not supported.".format(f=filter_type))

pandas/tests/frame/test_analytics.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,6 @@ def assert_bool_op_api(opname, bool_frame_with_na, float_string_frame,
228228
getattr(mixed, opname)(axis=0)
229229
getattr(mixed, opname)(axis=1)
230230

231-
class NonzeroFail(object):
232-
233-
def __nonzero__(self):
234-
raise ValueError
235-
236-
mixed['_nonzero_fail_'] = NonzeroFail()
237-
238231
if has_bool_only:
239232
getattr(mixed, opname)(axis=0, bool_only=True)
240233
getattr(mixed, opname)(axis=1, bool_only=True)
@@ -1374,25 +1367,22 @@ def test_any_all_extra(self):
13741367
result = df[['C']].all(axis=None).item()
13751368
assert result is True
13761369

1377-
# skip pathological failure cases
1378-
# class CantNonzero(object):
1379-
1380-
# def __nonzero__(self):
1381-
# raise ValueError
1370+
def test_any_datetime(self):
13821371

1383-
# df[4] = CantNonzero()
1384-
1385-
# it works!
1386-
# df.any(1)
1387-
# df.all(1)
1388-
# df.any(1, bool_only=True)
1389-
# df.all(1, bool_only=True)
1372+
# GH 23070
1373+
float_data = [1, np.nan, 3, np.nan]
1374+
datetime_data = [pd.Timestamp('1960-02-15'),
1375+
pd.Timestamp('1960-02-16'),
1376+
pd.NaT,
1377+
pd.NaT]
1378+
df = DataFrame({
1379+
"A": float_data,
1380+
"B": datetime_data
1381+
})
13901382

1391-
# df[4][4] = np.nan
1392-
# df.any(1)
1393-
# df.all(1)
1394-
# df.any(1, bool_only=True)
1395-
# df.all(1, bool_only=True)
1383+
result = df.any(1)
1384+
expected = Series([True, True, True, False])
1385+
tm.assert_series_equal(result, expected)
13961386

13971387
@pytest.mark.parametrize('func, data, expected', [
13981388
(np.any, {}, False),

0 commit comments

Comments
 (0)