@@ -66,69 +66,78 @@ def test_sample_lengths(self, obj):
66
66
67
67
def test_sample_invalid_random_state (self , obj ):
68
68
# Check for error when random_state argument invalid.
69
- with pytest .raises (ValueError ):
70
- obj .sample (random_state = "astring!" )
69
+ msg = (
70
+ "random_state must be an integer, array-like, a BitGenerator, a numpy "
71
+ "RandomState, or None"
72
+ )
73
+ with pytest .raises (ValueError , match = msg ):
74
+ obj .sample (random_state = "a_string" )
71
75
72
76
def test_sample_wont_accept_n_and_frac (self , obj ):
73
77
# Giving both frac and N throws error
74
- with pytest .raises (ValueError ):
78
+ msg = "Please enter a value for `frac` OR `n`, not both"
79
+ with pytest .raises (ValueError , match = msg ):
75
80
obj .sample (n = 3 , frac = 0.3 )
76
81
77
82
def test_sample_requires_positive_n_frac (self , obj ):
78
- with pytest .raises (ValueError ):
83
+ msg = "A negative number of rows requested. Please provide positive value."
84
+ with pytest .raises (ValueError , match = msg ):
79
85
obj .sample (n = - 3 )
80
- with pytest .raises (ValueError ):
86
+ with pytest .raises (ValueError , match = msg ):
81
87
obj .sample (frac = - 0.3 )
82
88
83
89
def test_sample_requires_integer_n (self , obj ):
84
90
# Make sure float values of `n` give error
85
- with pytest .raises (ValueError ):
91
+ with pytest .raises (ValueError , match = "Only integers accepted as `n` values" ):
86
92
obj .sample (n = 3.2 )
87
93
88
94
def test_sample_invalid_weight_lengths (self , obj ):
89
95
# Weight length must be right
90
- with pytest .raises (ValueError ):
96
+ msg = "Weights and axis to be sampled must be of same length"
97
+ with pytest .raises (ValueError , match = msg ):
91
98
obj .sample (n = 3 , weights = [0 , 1 ])
92
99
93
- with pytest .raises (ValueError ):
100
+ with pytest .raises (ValueError , match = msg ):
94
101
bad_weights = [0.5 ] * 11
95
102
obj .sample (n = 3 , weights = bad_weights )
96
103
97
- with pytest .raises (ValueError ):
104
+ with pytest .raises (ValueError , match = "Fewer non-zero entries in p than size" ):
98
105
bad_weight_series = Series ([0 , 0 , 0.2 ])
99
106
obj .sample (n = 4 , weights = bad_weight_series )
100
107
101
108
def test_sample_negative_weights (self , obj ):
102
109
# Check won't accept negative weights
103
- with pytest .raises (ValueError ):
104
- bad_weights = [- 0.1 ] * 10
110
+ bad_weights = [- 0.1 ] * 10
111
+ msg = "weight vector many not include negative values"
112
+ with pytest .raises (ValueError , match = msg ):
105
113
obj .sample (n = 3 , weights = bad_weights )
106
114
107
115
def test_sample_inf_weights (self , obj ):
108
116
# Check inf and -inf throw errors:
109
117
110
- with pytest .raises (ValueError ):
111
- weights_with_inf = [0.1 ] * 10
112
- weights_with_inf [0 ] = np .inf
118
+ weights_with_inf = [0.1 ] * 10
119
+ weights_with_inf [0 ] = np .inf
120
+ msg = "weight vector may not include `inf` values"
121
+ with pytest .raises (ValueError , match = msg ):
113
122
obj .sample (n = 3 , weights = weights_with_inf )
114
123
115
- with pytest . raises ( ValueError ):
116
- weights_with_ninf = [ 0.1 ] * 10
117
- weights_with_ninf [ 0 ] = - np . inf
124
+ weights_with_ninf = [ 0.1 ] * 10
125
+ weights_with_ninf [ 0 ] = - np . inf
126
+ with pytest . raises ( ValueError , match = msg ):
118
127
obj .sample (n = 3 , weights = weights_with_ninf )
119
128
120
129
def test_sample_zero_weights (self , obj ):
121
130
# All zeros raises errors
122
131
123
132
zero_weights = [0 ] * 10
124
- with pytest .raises (ValueError ):
133
+ with pytest .raises (ValueError , match = "Invalid weights: weights sum to zero" ):
125
134
obj .sample (n = 3 , weights = zero_weights )
126
135
127
136
def test_sample_missing_weights (self , obj ):
128
137
# All missing weights
129
138
130
139
nan_weights = [np .nan ] * 10
131
- with pytest .raises (ValueError ):
140
+ with pytest .raises (ValueError , match = "Invalid weights: weights sum to zero" ):
132
141
obj .sample (n = 3 , weights = nan_weights )
133
142
134
143
def test_sample_none_weights (self , obj ):
@@ -205,10 +214,15 @@ def test_sample(self):
205
214
# Ensure proper error if string given as weight for Series or
206
215
# DataFrame with axis = 1.
207
216
ser = Series (range (10 ))
208
- with pytest .raises (ValueError ):
217
+ msg = "Strings cannot be passed as weights when sampling from a Series."
218
+ with pytest .raises (ValueError , match = msg ):
209
219
ser .sample (n = 3 , weights = "weight_column" )
210
220
211
- with pytest .raises (ValueError ):
221
+ msg = (
222
+ "Strings can only be passed to weights when sampling from rows on a "
223
+ "DataFrame"
224
+ )
225
+ with pytest .raises (ValueError , match = msg ):
212
226
df .sample (n = 1 , weights = "weight_column" , axis = 1 )
213
227
214
228
# Check weighting key error
@@ -246,18 +260,21 @@ def test_sample(self):
246
260
)
247
261
248
262
# Check out of range axis values
249
- with pytest .raises (ValueError ):
263
+ msg = "No axis named 2 for object type DataFrame"
264
+ with pytest .raises (ValueError , match = msg ):
250
265
df .sample (n = 1 , axis = 2 )
251
266
252
- with pytest .raises (ValueError ):
267
+ msg = "No axis named not_a_name for object type DataFrame"
268
+ with pytest .raises (ValueError , match = msg ):
253
269
df .sample (n = 1 , axis = "not_a_name" )
254
270
255
- with pytest . raises ( ValueError ):
256
- ser = Series ( range ( 10 ))
271
+ ser = Series ( range ( 10 ))
272
+ with pytest . raises ( ValueError , match = "No axis named 1 for object type Series" ):
257
273
ser .sample (n = 1 , axis = 1 )
258
274
259
275
# Test weight length compared to correct axis
260
- with pytest .raises (ValueError ):
276
+ msg = "Weights and axis to be sampled must be of same length"
277
+ with pytest .raises (ValueError , match = msg ):
261
278
df .sample (n = 1 , axis = 1 , weights = [0.5 ] * 10 )
262
279
263
280
def test_sample_axis1 (self ):
@@ -294,7 +311,8 @@ def test_sample_aligns_weights_with_frame(self):
294
311
295
312
# No overlap in weight and sampled DataFrame indices
296
313
ser4 = Series ([1 , 0 ], index = [1 , 2 ])
297
- with pytest .raises (ValueError ):
314
+
315
+ with pytest .raises (ValueError , match = "Invalid weights: weights sum to zero" ):
298
316
df .sample (1 , weights = ser4 )
299
317
300
318
def test_sample_is_copy (self ):
0 commit comments