@@ -205,11 +205,31 @@ def test_get_numeric_data_preserve_dtype(self):
205
205
206
206
def test_nonzero_single_element (self ):
207
207
208
+ # allow single item via bool method
208
209
s = Series ([True ])
209
- self .assertRaises ( ValueError , lambda : bool (s ) )
210
+ self .assert_ ( s . bool () is True )
210
211
211
212
s = Series ([False ])
212
- self .assertRaises (ValueError , lambda : bool (s ))
213
+ self .assert_ (s .bool () is False )
214
+
215
+ # single item nan to raise
216
+ for s in [ Series ([np .nan ]), Series ([pd .NaT ]), Series ([True ]), Series ([False ]) ]:
217
+ self .assertRaises (ValueError , lambda : bool (s ))
218
+
219
+ for s in [ Series ([np .nan ]), Series ([pd .NaT ])]:
220
+ self .assertRaises (ValueError , lambda : s .bool ())
221
+
222
+ # multiple bool are still an error
223
+ for s in [Series ([True ,True ]), Series ([False , False ])]:
224
+ self .assertRaises (ValueError , lambda : bool (s ))
225
+ self .assertRaises (ValueError , lambda : s .bool ())
226
+
227
+ # single non-bool are an error
228
+ for s in [Series ([1 ]), Series ([0 ]),
229
+ Series (['a' ]), Series ([0.0 ])]:
230
+ self .assertRaises (ValueError , lambda : bool (s ))
231
+ self .assertRaises (ValueError , lambda : s .bool ())
232
+
213
233
214
234
class TestDataFrame (unittest .TestCase , Generic ):
215
235
_typ = DataFrame
@@ -220,6 +240,19 @@ def test_rename_mi(self):
220
240
index = MultiIndex .from_tuples ([("A" ,x ) for x in ["a" ,"B" ,"c" ]]))
221
241
result = df .rename (str .lower )
222
242
243
+ def test_nonzero_single_element (self ):
244
+
245
+ # allow single item via bool method
246
+ df = DataFrame ([[True ]])
247
+ self .assert_ (df .bool () is True )
248
+
249
+ df = DataFrame ([[False ]])
250
+ self .assert_ (df .bool () is False )
251
+
252
+ df = DataFrame ([[False , False ]])
253
+ self .assertRaises (ValueError , lambda : df .bool ())
254
+ self .assertRaises (ValueError , lambda : bool (df ))
255
+
223
256
def test_get_numeric_data_preserve_dtype (self ):
224
257
225
258
# get the numeric data
0 commit comments