15
15
from pandas .compat import lrange , PY35
16
16
from pandas import (compat , isna , notna , DataFrame , Series ,
17
17
MultiIndex , date_range , Timestamp , Categorical ,
18
- _np_version_under1p12 , _np_version_under1p15 )
18
+ _np_version_under1p12 )
19
19
import pandas as pd
20
20
import pandas .core .nanops as nanops
21
21
import pandas .core .algorithms as algorithms
@@ -1139,11 +1139,35 @@ def test_any_all(self):
1139
1139
self ._check_bool_op ('any' , np .any , has_skipna = True , has_bool_only = True )
1140
1140
self ._check_bool_op ('all' , np .all , has_skipna = True , has_bool_only = True )
1141
1141
1142
- df = DataFrame (randn (10 , 4 )) > 0
1143
- df .any (1 )
1144
- df .all (1 )
1145
- df .any (1 , bool_only = True )
1146
- df .all (1 , bool_only = True )
1142
+ def test_any_all_extra (self ):
1143
+ df = DataFrame ({
1144
+ 'A' : [True , False , False ],
1145
+ 'B' : [True , True , False ],
1146
+ 'C' : [True , True , True ],
1147
+ }, index = ['a' , 'b' , 'c' ])
1148
+ result = df [['A' , 'B' ]].any (1 )
1149
+ expected = Series ([True , True , False ], index = ['a' , 'b' , 'c' ])
1150
+ tm .assert_series_equal (result , expected )
1151
+
1152
+ result = df [['A' , 'B' ]].any (1 , bool_only = True )
1153
+ tm .assert_series_equal (result , expected )
1154
+
1155
+ result = df .all (1 )
1156
+ expected = Series ([True , False , False ], index = ['a' , 'b' , 'c' ])
1157
+ tm .assert_series_equal (result , expected )
1158
+
1159
+ result = df .all (1 , bool_only = True )
1160
+ tm .assert_series_equal (result , expected )
1161
+
1162
+ # Axis is None
1163
+ result = df .all (axis = None ).item ()
1164
+ assert result is False
1165
+
1166
+ result = df .any (axis = None ).item ()
1167
+ assert result is True
1168
+
1169
+ result = df [['C' ]].all (axis = None ).item ()
1170
+ assert result is True
1147
1171
1148
1172
# skip pathological failure cases
1149
1173
# class CantNonzero(object):
@@ -1165,6 +1189,86 @@ def test_any_all(self):
1165
1189
# df.any(1, bool_only=True)
1166
1190
# df.all(1, bool_only=True)
1167
1191
1192
+ @pytest .mark .parametrize ('func, data, expected' , [
1193
+ (np .any , {}, False ),
1194
+ (np .all , {}, True ),
1195
+ (np .any , {'A' : []}, False ),
1196
+ (np .all , {'A' : []}, True ),
1197
+ (np .any , {'A' : [False , False ]}, False ),
1198
+ (np .all , {'A' : [False , False ]}, False ),
1199
+ (np .any , {'A' : [True , False ]}, True ),
1200
+ (np .all , {'A' : [True , False ]}, False ),
1201
+ (np .any , {'A' : [True , True ]}, True ),
1202
+ (np .all , {'A' : [True , True ]}, True ),
1203
+
1204
+ (np .any , {'A' : [False ], 'B' : [False ]}, False ),
1205
+ (np .all , {'A' : [False ], 'B' : [False ]}, False ),
1206
+
1207
+ (np .any , {'A' : [False , False ], 'B' : [False , True ]}, True ),
1208
+ (np .all , {'A' : [False , False ], 'B' : [False , True ]}, False ),
1209
+
1210
+ # other types
1211
+ (np .all , {'A' : pd .Series ([0.0 , 1.0 ], dtype = 'float' )}, False ),
1212
+ (np .any , {'A' : pd .Series ([0.0 , 1.0 ], dtype = 'float' )}, True ),
1213
+ (np .all , {'A' : pd .Series ([0 , 1 ], dtype = int )}, False ),
1214
+ (np .any , {'A' : pd .Series ([0 , 1 ], dtype = int )}, True ),
1215
+ pytest .param (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'M8[ns]' )}, False ,
1216
+ marks = [td .skip_if_np_lt_115 ]),
1217
+ pytest .param (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'M8[ns]' )}, True ,
1218
+ marks = [td .skip_if_np_lt_115 ]),
1219
+ pytest .param (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'M8[ns]' )}, True ,
1220
+ marks = [td .skip_if_np_lt_115 ]),
1221
+ pytest .param (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'M8[ns]' )}, True ,
1222
+ marks = [td .skip_if_np_lt_115 ]),
1223
+ pytest .param (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'm8[ns]' )}, False ,
1224
+ marks = [td .skip_if_np_lt_115 ]),
1225
+ pytest .param (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'm8[ns]' )}, True ,
1226
+ marks = [td .skip_if_np_lt_115 ]),
1227
+ pytest .param (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'm8[ns]' )}, True ,
1228
+ marks = [td .skip_if_np_lt_115 ]),
1229
+ pytest .param (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'm8[ns]' )}, True ,
1230
+ marks = [td .skip_if_np_lt_115 ]),
1231
+ (np .all , {'A' : pd .Series ([0 , 1 ], dtype = 'category' )}, False ),
1232
+ (np .any , {'A' : pd .Series ([0 , 1 ], dtype = 'category' )}, True ),
1233
+ (np .all , {'A' : pd .Series ([1 , 2 ], dtype = 'category' )}, True ),
1234
+ (np .any , {'A' : pd .Series ([1 , 2 ], dtype = 'category' )}, True ),
1235
+
1236
+ # # Mix
1237
+ # GH-21484
1238
+ # (np.all, {'A': pd.Series([10, 20], dtype='M8[ns]'),
1239
+ # 'B': pd.Series([10, 20], dtype='m8[ns]')}, True),
1240
+ ])
1241
+ def test_any_all_np_func (self , func , data , expected ):
1242
+ # https://github.com/pandas-dev/pandas/issues/19976
1243
+ data = DataFrame (data )
1244
+ result = func (data )
1245
+ assert isinstance (result , np .bool_ )
1246
+ assert result .item () is expected
1247
+
1248
+ # method version
1249
+ result = getattr (DataFrame (data ), func .__name__ )(axis = None )
1250
+ assert isinstance (result , np .bool_ )
1251
+ assert result .item () is expected
1252
+
1253
+ def test_any_all_object (self ):
1254
+ # https://github.com/pandas-dev/pandas/issues/19976
1255
+ result = np .all (DataFrame (columns = ['a' , 'b' ])).item ()
1256
+ assert result is True
1257
+
1258
+ result = np .any (DataFrame (columns = ['a' , 'b' ])).item ()
1259
+ assert result is False
1260
+
1261
+ @pytest .mark .parametrize ('method' , ['any' , 'all' ])
1262
+ def test_any_all_level_axis_none_raises (self , method ):
1263
+ df = DataFrame (
1264
+ {"A" : 1 },
1265
+ index = MultiIndex .from_product ([['A' , 'B' ], ['a' , 'b' ]],
1266
+ names = ['out' , 'in' ])
1267
+ )
1268
+ xpr = "Must specify 'axis' when aggregating by level."
1269
+ with tm .assert_raises_regex (ValueError , xpr ):
1270
+ getattr (df , method )(axis = None , level = 'out' )
1271
+
1168
1272
def _check_bool_op (self , name , alternative , frame = None , has_skipna = True ,
1169
1273
has_bool_only = False ):
1170
1274
if frame is None :
@@ -2071,9 +2175,6 @@ def test_clip_against_list_like(self, inplace, lower, axis, res):
2071
2175
result = original
2072
2176
tm .assert_frame_equal (result , expected , check_exact = True )
2073
2177
2074
- @pytest .mark .xfail (
2075
- not _np_version_under1p15 ,
2076
- reason = "failing under numpy-dev gh-19976" )
2077
2178
@pytest .mark .parametrize ("axis" , [0 , 1 , None ])
2078
2179
def test_clip_against_frame (self , axis ):
2079
2180
df = DataFrame (np .random .randn (1000 , 2 ))
0 commit comments