@@ -73,7 +73,6 @@ def _construct(self, shape, value=None, **kwargs):
73
73
arr = np .random .randn (* shape )
74
74
return self ._typ (arr ,** kwargs )
75
75
76
-
77
76
def _compare (self , result , expected ):
78
77
self ._comparator (result ,expected )
79
78
@@ -82,14 +81,14 @@ def test_rename(self):
82
81
# single axis
83
82
for axis in self ._axes ():
84
83
kwargs = { axis : list ('ABCD' ) }
85
- o = self ._construct (4 ,** kwargs )
84
+ obj = self ._construct (4 ,** kwargs )
86
85
87
86
# no values passed
88
87
#self.assertRaises(Exception, o.rename(str.lower))
89
88
90
89
# rename a single axis
91
- result = o .rename (** { axis : str .lower })
92
- expected = o .copy ()
90
+ result = obj .rename (** { axis : str .lower })
91
+ expected = obj .copy ()
93
92
setattr (expected ,axis ,list ('abcd' ))
94
93
self ._compare (result , expected )
95
94
@@ -119,6 +118,41 @@ def test_get_numeric_data(self):
119
118
self ._compare (result , o )
120
119
121
120
# _get_numeric_data is includes _get_bool_data, so can't test for non-inclusion
121
+ def test_nonzero (self ):
122
+
123
+ # GH 4633
124
+ # look at the boolean/nonzero behavior for objects
125
+ obj = self ._construct (shape = 4 )
126
+ self .assertRaises (ValueError , lambda : bool (obj == 0 ))
127
+ self .assertRaises (ValueError , lambda : bool (obj == 1 ))
128
+ self .assertRaises (ValueError , lambda : bool (obj ))
129
+
130
+ obj = self ._construct (shape = 4 ,value = 1 )
131
+ self .assertRaises (ValueError , lambda : bool (obj == 0 ))
132
+ self .assertRaises (ValueError , lambda : bool (obj == 1 ))
133
+ self .assertRaises (ValueError , lambda : bool (obj ))
134
+
135
+ obj = self ._construct (shape = 4 ,value = np .nan )
136
+ self .assertRaises (ValueError , lambda : bool (obj == 0 ))
137
+ self .assertRaises (ValueError , lambda : bool (obj == 1 ))
138
+ self .assertRaises (ValueError , lambda : bool (obj ))
139
+
140
+ # empty
141
+ obj = self ._construct (shape = 0 )
142
+ self .assertRaises (ValueError , lambda : bool (obj ))
143
+
144
+ # invalid behaviors
145
+
146
+ obj1 = self ._construct (shape = 4 ,value = 1 )
147
+ obj2 = self ._construct (shape = 4 ,value = 1 )
148
+
149
+ def f ():
150
+ if obj1 :
151
+ print ("this works and shouldn't" )
152
+ self .assertRaises (ValueError , f )
153
+ self .assertRaises (ValueError , lambda : obj1 and obj2 )
154
+ self .assertRaises (ValueError , lambda : obj1 or obj2 )
155
+ self .assertRaises (ValueError , lambda : not obj1 )
122
156
123
157
class TestSeries (unittest .TestCase , Generic ):
124
158
_typ = Series
@@ -154,6 +188,14 @@ def test_get_numeric_data_preserve_dtype(self):
154
188
expected = Series ([],dtype = 'M8[ns]' )
155
189
self ._compare (result , expected )
156
190
191
+ def test_nonzero_single_element (self ):
192
+
193
+ s = Series ([True ])
194
+ self .assertRaises (ValueError , lambda : bool (s ))
195
+
196
+ s = Series ([False ])
197
+ self .assertRaises (ValueError , lambda : bool (s ))
198
+
157
199
class TestDataFrame (unittest .TestCase , Generic ):
158
200
_typ = DataFrame
159
201
_comparator = lambda self , x , y : assert_frame_equal (x ,y )
0 commit comments