1515from pandas .compat import lrange , PY35
1616from pandas import (compat , isna , notna , DataFrame , Series ,
1717 MultiIndex , date_range , Timestamp , Categorical ,
18- _np_version_under1p12 , _np_version_under1p15 ,
18+ _np_version_under1p12 ,
1919 to_datetime , to_timedelta )
2020import pandas as pd
2121import pandas .core .nanops as nanops
@@ -1159,11 +1159,35 @@ def test_any_all(self):
11591159 self ._check_bool_op ('any' , np .any , has_skipna = True , has_bool_only = True )
11601160 self ._check_bool_op ('all' , np .all , has_skipna = True , has_bool_only = True )
11611161
1162- df = DataFrame (randn (10 , 4 )) > 0
1163- df .any (1 )
1164- df .all (1 )
1165- df .any (1 , bool_only = True )
1166- df .all (1 , bool_only = True )
1162+ def test_any_all_extra (self ):
1163+ df = DataFrame ({
1164+ 'A' : [True , False , False ],
1165+ 'B' : [True , True , False ],
1166+ 'C' : [True , True , True ],
1167+ }, index = ['a' , 'b' , 'c' ])
1168+ result = df [['A' , 'B' ]].any (1 )
1169+ expected = Series ([True , True , False ], index = ['a' , 'b' , 'c' ])
1170+ tm .assert_series_equal (result , expected )
1171+
1172+ result = df [['A' , 'B' ]].any (1 , bool_only = True )
1173+ tm .assert_series_equal (result , expected )
1174+
1175+ result = df .all (1 )
1176+ expected = Series ([True , False , False ], index = ['a' , 'b' , 'c' ])
1177+ tm .assert_series_equal (result , expected )
1178+
1179+ result = df .all (1 , bool_only = True )
1180+ tm .assert_series_equal (result , expected )
1181+
1182+ # Axis is None
1183+ result = df .all (axis = None ).item ()
1184+ assert result is False
1185+
1186+ result = df .any (axis = None ).item ()
1187+ assert result is True
1188+
1189+ result = df [['C' ]].all (axis = None ).item ()
1190+ assert result is True
11671191
11681192 # skip pathological failure cases
11691193 # class CantNonzero(object):
@@ -1185,6 +1209,86 @@ def test_any_all(self):
11851209 # df.any(1, bool_only=True)
11861210 # df.all(1, bool_only=True)
11871211
1212+ @pytest .mark .parametrize ('func, data, expected' , [
1213+ (np .any , {}, False ),
1214+ (np .all , {}, True ),
1215+ (np .any , {'A' : []}, False ),
1216+ (np .all , {'A' : []}, True ),
1217+ (np .any , {'A' : [False , False ]}, False ),
1218+ (np .all , {'A' : [False , False ]}, False ),
1219+ (np .any , {'A' : [True , False ]}, True ),
1220+ (np .all , {'A' : [True , False ]}, False ),
1221+ (np .any , {'A' : [True , True ]}, True ),
1222+ (np .all , {'A' : [True , True ]}, True ),
1223+
1224+ (np .any , {'A' : [False ], 'B' : [False ]}, False ),
1225+ (np .all , {'A' : [False ], 'B' : [False ]}, False ),
1226+
1227+ (np .any , {'A' : [False , False ], 'B' : [False , True ]}, True ),
1228+ (np .all , {'A' : [False , False ], 'B' : [False , True ]}, False ),
1229+
1230+ # other types
1231+ (np .all , {'A' : pd .Series ([0.0 , 1.0 ], dtype = 'float' )}, False ),
1232+ (np .any , {'A' : pd .Series ([0.0 , 1.0 ], dtype = 'float' )}, True ),
1233+ (np .all , {'A' : pd .Series ([0 , 1 ], dtype = int )}, False ),
1234+ (np .any , {'A' : pd .Series ([0 , 1 ], dtype = int )}, True ),
1235+ pytest .param (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'M8[ns]' )}, False ,
1236+ marks = [td .skip_if_np_lt_115 ]),
1237+ pytest .param (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'M8[ns]' )}, True ,
1238+ marks = [td .skip_if_np_lt_115 ]),
1239+ pytest .param (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'M8[ns]' )}, True ,
1240+ marks = [td .skip_if_np_lt_115 ]),
1241+ pytest .param (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'M8[ns]' )}, True ,
1242+ marks = [td .skip_if_np_lt_115 ]),
1243+ pytest .param (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'm8[ns]' )}, False ,
1244+ marks = [td .skip_if_np_lt_115 ]),
1245+ pytest .param (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'm8[ns]' )}, True ,
1246+ marks = [td .skip_if_np_lt_115 ]),
1247+ pytest .param (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'm8[ns]' )}, True ,
1248+ marks = [td .skip_if_np_lt_115 ]),
1249+ pytest .param (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'm8[ns]' )}, True ,
1250+ marks = [td .skip_if_np_lt_115 ]),
1251+ (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'category' )}, False ),
1252+ (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'category' )}, True ),
1253+ (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'category' )}, True ),
1254+ (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'category' )}, True ),
1255+
1256+ # # Mix
1257+ # GH-21484
1258+ # (np.all, {'A': pd.Series([10, 20], dtype='M8[ns]'),
1259+ # 'B': pd.Series([10, 20], dtype='m8[ns]')}, True),
1260+ ])
1261+ def test_any_all_np_func (self , func , data , expected ):
1262+ # https://github.com/pandas-dev/pandas/issues/19976
1263+ data = DataFrame (data )
1264+ result = func (data )
1265+ assert isinstance (result , np .bool_ )
1266+ assert result .item () is expected
1267+
1268+ # method version
1269+ result = getattr (DataFrame (data ), func .__name__ )(axis = None )
1270+ assert isinstance (result , np .bool_ )
1271+ assert result .item () is expected
1272+
1273+ def test_any_all_object (self ):
1274+ # https://github.com/pandas-dev/pandas/issues/19976
1275+ result = np .all (DataFrame (columns = ['a' , 'b' ])).item ()
1276+ assert result is True
1277+
1278+ result = np .any (DataFrame (columns = ['a' , 'b' ])).item ()
1279+ assert result is False
1280+
1281+ @pytest .mark .parametrize ('method' , ['any' , 'all' ])
1282+ def test_any_all_level_axis_none_raises (self , method ):
1283+ df = DataFrame (
1284+ {"A" : 1 },
1285+ index = MultiIndex .from_product ([['A' , 'B' ], ['a' , 'b' ]],
1286+ names = ['out' , 'in' ])
1287+ )
1288+ xpr = "Must specify 'axis' when aggregating by level."
1289+ with tm .assert_raises_regex (ValueError , xpr ):
1290+ getattr (df , method )(axis = None , level = 'out' )
1291+
11881292 def _check_bool_op (self , name , alternative , frame = None , has_skipna = True ,
11891293 has_bool_only = False ):
11901294 if frame is None :
@@ -2091,9 +2195,6 @@ def test_clip_against_list_like(self, inplace, lower, axis, res):
20912195 result = original
20922196 tm .assert_frame_equal (result , expected , check_exact = True )
20932197
2094- @pytest .mark .xfail (
2095- not _np_version_under1p15 ,
2096- reason = "failing under numpy-dev gh-19976" )
20972198 @pytest .mark .parametrize ("axis" , [0 , 1 , None ])
20982199 def test_clip_against_frame (self , axis ):
20992200 df = DataFrame (np .random .randn (1000 , 2 ))
0 commit comments