@@ -66,69 +66,78 @@ def test_sample_lengths(self, obj):
6666
6767 def test_sample_invalid_random_state (self , obj ):
6868 # 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" )
7175
7276 def test_sample_wont_accept_n_and_frac (self , obj ):
7377 # 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 ):
7580 obj .sample (n = 3 , frac = 0.3 )
7681
7782 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 ):
7985 obj .sample (n = - 3 )
80- with pytest .raises (ValueError ):
86+ with pytest .raises (ValueError , match = msg ):
8187 obj .sample (frac = - 0.3 )
8288
8389 def test_sample_requires_integer_n (self , obj ):
8490 # 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" ):
8692 obj .sample (n = 3.2 )
8793
8894 def test_sample_invalid_weight_lengths (self , obj ):
8995 # 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 ):
9198 obj .sample (n = 3 , weights = [0 , 1 ])
9299
93- with pytest .raises (ValueError ):
100+ with pytest .raises (ValueError , match = msg ):
94101 bad_weights = [0.5 ] * 11
95102 obj .sample (n = 3 , weights = bad_weights )
96103
97- with pytest .raises (ValueError ):
104+ with pytest .raises (ValueError , match = "Fewer non-zero entries in p than size" ):
98105 bad_weight_series = Series ([0 , 0 , 0.2 ])
99106 obj .sample (n = 4 , weights = bad_weight_series )
100107
101108 def test_sample_negative_weights (self , obj ):
102109 # 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 ):
105113 obj .sample (n = 3 , weights = bad_weights )
106114
107115 def test_sample_inf_weights (self , obj ):
108116 # Check inf and -inf throw errors:
109117
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 ):
113122 obj .sample (n = 3 , weights = weights_with_inf )
114123
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 ):
118127 obj .sample (n = 3 , weights = weights_with_ninf )
119128
120129 def test_sample_zero_weights (self , obj ):
121130 # All zeros raises errors
122131
123132 zero_weights = [0 ] * 10
124- with pytest .raises (ValueError ):
133+ with pytest .raises (ValueError , match = "Invalid weights: weights sum to zero" ):
125134 obj .sample (n = 3 , weights = zero_weights )
126135
127136 def test_sample_missing_weights (self , obj ):
128137 # All missing weights
129138
130139 nan_weights = [np .nan ] * 10
131- with pytest .raises (ValueError ):
140+ with pytest .raises (ValueError , match = "Invalid weights: weights sum to zero" ):
132141 obj .sample (n = 3 , weights = nan_weights )
133142
134143 def test_sample_none_weights (self , obj ):
@@ -205,10 +214,15 @@ def test_sample(self):
205214 # Ensure proper error if string given as weight for Series or
206215 # DataFrame with axis = 1.
207216 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 ):
209219 ser .sample (n = 3 , weights = "weight_column" )
210220
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 ):
212226 df .sample (n = 1 , weights = "weight_column" , axis = 1 )
213227
214228 # Check weighting key error
@@ -246,18 +260,21 @@ def test_sample(self):
246260 )
247261
248262 # 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 ):
250265 df .sample (n = 1 , axis = 2 )
251266
252- with pytest .raises (ValueError ):
267+ msg = "No axis named not_a_name for object type DataFrame"
268+ with pytest .raises (ValueError , match = msg ):
253269 df .sample (n = 1 , axis = "not_a_name" )
254270
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" ):
257273 ser .sample (n = 1 , axis = 1 )
258274
259275 # 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 ):
261278 df .sample (n = 1 , axis = 1 , weights = [0.5 ] * 10 )
262279
263280 def test_sample_axis1 (self ):
@@ -294,7 +311,8 @@ def test_sample_aligns_weights_with_frame(self):
294311
295312 # No overlap in weight and sampled DataFrame indices
296313 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" ):
298316 df .sample (1 , weights = ser4 )
299317
300318 def test_sample_is_copy (self ):
0 commit comments