@@ -431,10 +431,6 @@ def test_sample(self):
431
431
weights_with_ninf [0 ] = - np .inf
432
432
o .sample (n = 3 , weights = weights_with_ninf )
433
433
434
- # Ensure proper error if string given as weight for Series
435
- s = Series (range (10 ))
436
- with tm .assertRaises (ValueError ):
437
- s .sample (n = 3 , weights = 'weight_column' )
438
434
439
435
# A few dataframe test with degenerate weights.
440
436
easy_weight_list = [0 ]* 10
@@ -443,33 +439,36 @@ def test_sample(self):
443
439
df = pd .DataFrame ({'col1' :range (10 ,20 ),
444
440
'col2' :range (20 ,30 ),
445
441
'colString' : ['a' ]* 10 ,
446
- 'easyweights' :easy_weight_list })
442
+ 'easyweights' :easy_weight_list }, index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ] )
447
443
sample1 = df .sample (n = 1 , weights = 'easyweights' )
448
444
assert_frame_equal (sample1 , df .iloc [5 :6 ])
449
445
446
+ # Ensure proper error if string given as weight for Series, panel, or
447
+ # DataFrame with axis = 1.
448
+ s = Series (range (10 ))
449
+ with tm .assertRaises (ValueError ):
450
+ s .sample (n = 3 , weights = 'weight_column' )
451
+
452
+ panel = pd .Panel (items = [0 ,1 ,2 ], major_axis = [2 ,3 ,4 ], minor_axis = [3 ,4 ,5 ])
453
+ with tm .assertRaises (ValueError ):
454
+ panel .sample (n = 1 , weights = 'weight_column' )
455
+
456
+ with tm .assertRaises (ValueError ):
457
+ df .sample (n = 1 , weights = 'weight_column' , axis = 1 )
458
+
450
459
# Check weighting key error
451
460
with tm .assertRaises (KeyError ):
452
461
df .sample (n = 3 , weights = 'not_a_real_column_name' )
453
462
454
463
# Check np.nan are replaced by zeros.
455
464
weights_with_nan = [np .nan ]* 10
456
465
weights_with_nan [5 ] = 0.5
457
-
458
- sampled_df = df .sample (n = 1 , weights = weights_with_nan )
459
- tm .assert_frame_equal (sampled_df , df .iloc [5 :6 ])
460
-
461
- sampled_s = s .sample (n = 1 , weights = weights_with_nan )
462
- tm .assert_series_equal (sampled_s , s .iloc [5 :6 ])
466
+ self ._compare (o .sample (n = 1 , weights = weights_with_nan ), o .iloc [5 :6 ])
463
467
464
468
# Check None are also replaced by zeros.
465
469
weights_with_None = [None ]* 10
466
470
weights_with_None [5 ] = 0.5
467
-
468
- sampled_df2 = df .sample (n = 1 , weights = weights_with_None )
469
- tm .assert_frame_equal (sampled_df2 , df .iloc [5 :6 ])
470
-
471
- sampled_s2 = s .sample (n = 1 , weights = weights_with_None )
472
- tm .assert_series_equal (sampled_s2 , s .iloc [5 :6 ])
471
+ self ._compare (o .sample (n = 1 , weights = weights_with_None ), o .iloc [5 :6 ])
473
472
474
473
# Check that re-normalizes weights that don't sum to one.
475
474
weights_less_than_1 = [0 ]* 10
0 commit comments