@@ -444,7 +444,11 @@ def test_sum(self):
444
444
has_numeric_only = True , check_dtype = False ,
445
445
check_less_precise = True )
446
446
447
- def test_stat_operators_attempt_obj_array (self ):
447
+ @pytest .mark .parametrize (
448
+ "method" , ['sum' , 'mean' , 'prod' , 'var' ,
449
+ 'std' , 'skew' , 'min' , 'max' ])
450
+ def test_stat_operators_attempt_obj_array (self , method ):
451
+ # GH #676
448
452
data = {
449
453
'a' : [- 0.00049987540199591344 , - 0.0016467257772919831 ,
450
454
0.00067695870775883013 ],
@@ -454,20 +458,17 @@ def test_stat_operators_attempt_obj_array(self):
454
458
}
455
459
df1 = DataFrame (data , index = ['foo' , 'bar' , 'baz' ],
456
460
dtype = 'O' )
457
- methods = ['sum' , 'mean' , 'prod' , 'var' , 'std' , 'skew' , 'min' , 'max' ]
458
461
459
- # GH #676
460
462
df2 = DataFrame ({0 : [np .nan , 2 ], 1 : [np .nan , 3 ],
461
463
2 : [np .nan , 4 ]}, dtype = object )
462
464
463
465
for df in [df1 , df2 ]:
464
- for meth in methods :
465
- assert df .values .dtype == np .object_
466
- result = getattr (df , meth )(1 )
467
- expected = getattr (df .astype ('f8' ), meth )(1 )
466
+ assert df .values .dtype == np .object_
467
+ result = getattr (df , method )(1 )
468
+ expected = getattr (df .astype ('f8' ), method )(1 )
468
469
469
- if not tm . _incompat_bottleneck_version ( meth ) :
470
- tm .assert_series_equal (result , expected )
470
+ if method in [ 'sum' , 'prod' ] :
471
+ tm .assert_series_equal (result , expected )
471
472
472
473
def test_mean (self ):
473
474
self ._check_stat_op ('mean' , np .mean , check_dates = True )
@@ -559,15 +560,15 @@ def test_var_std(self):
559
560
arr = np .repeat (np .random .random ((1 , 1000 )), 1000 , 0 )
560
561
result = nanops .nanvar (arr , axis = 0 )
561
562
assert not (result < 0 ).any ()
562
- if nanops . _USE_BOTTLENECK :
563
- nanops . _USE_BOTTLENECK = False
563
+
564
+ with pd . option_context ( 'use_bottleneck' , False ):
564
565
result = nanops .nanvar (arr , axis = 0 )
565
566
assert not (result < 0 ).any ()
566
- nanops ._USE_BOTTLENECK = True
567
567
568
- def test_numeric_only_flag (self ):
568
+ @pytest .mark .parametrize (
569
+ "meth" , ['sem' , 'var' , 'std' ])
570
+ def test_numeric_only_flag (self , meth ):
569
571
# GH #9201
570
- methods = ['sem' , 'var' , 'std' ]
571
572
df1 = DataFrame (np .random .randn (5 , 3 ), columns = ['foo' , 'bar' , 'baz' ])
572
573
# set one entry to a number in str format
573
574
df1 .loc [0 , 'foo' ] = '100'
@@ -576,20 +577,19 @@ def test_numeric_only_flag(self):
576
577
# set one entry to a non-number str
577
578
df2 .loc [0 , 'foo' ] = 'a'
578
579
579
- for meth in methods :
580
- result = getattr (df1 , meth )(axis = 1 , numeric_only = True )
581
- expected = getattr (df1 [['bar' , 'baz' ]], meth )(axis = 1 )
582
- tm .assert_series_equal (expected , result )
580
+ result = getattr (df1 , meth )(axis = 1 , numeric_only = True )
581
+ expected = getattr (df1 [['bar' , 'baz' ]], meth )(axis = 1 )
582
+ tm .assert_series_equal (expected , result )
583
583
584
- result = getattr (df2 , meth )(axis = 1 , numeric_only = True )
585
- expected = getattr (df2 [['bar' , 'baz' ]], meth )(axis = 1 )
586
- tm .assert_series_equal (expected , result )
584
+ result = getattr (df2 , meth )(axis = 1 , numeric_only = True )
585
+ expected = getattr (df2 [['bar' , 'baz' ]], meth )(axis = 1 )
586
+ tm .assert_series_equal (expected , result )
587
587
588
- # df1 has all numbers, df2 has a letter inside
589
- pytest .raises (TypeError , lambda : getattr (df1 , meth )(
590
- axis = 1 , numeric_only = False ))
591
- pytest .raises (TypeError , lambda : getattr (df2 , meth )(
592
- axis = 1 , numeric_only = False ))
588
+ # df1 has all numbers, df2 has a letter inside
589
+ pytest .raises (TypeError , lambda : getattr (df1 , meth )(
590
+ axis = 1 , numeric_only = False ))
591
+ pytest .raises (TypeError , lambda : getattr (df2 , meth )(
592
+ axis = 1 , numeric_only = False ))
593
593
594
594
def test_mixed_ops (self ):
595
595
# GH 16116
@@ -602,11 +602,9 @@ def test_mixed_ops(self):
602
602
result = getattr (df , op )()
603
603
assert len (result ) == 2
604
604
605
- if nanops ._USE_BOTTLENECK :
606
- nanops ._USE_BOTTLENECK = False
605
+ with pd .option_context ('use_bottleneck' , False ):
607
606
result = getattr (df , op )()
608
607
assert len (result ) == 2
609
- nanops ._USE_BOTTLENECK = True
610
608
611
609
def test_cumsum (self ):
612
610
self .tsframe .loc [5 :10 , 0 ] = nan
@@ -672,11 +670,10 @@ def test_sem(self):
672
670
arr = np .repeat (np .random .random ((1 , 1000 )), 1000 , 0 )
673
671
result = nanops .nansem (arr , axis = 0 )
674
672
assert not (result < 0 ).any ()
675
- if nanops . _USE_BOTTLENECK :
676
- nanops . _USE_BOTTLENECK = False
673
+
674
+ with pd . option_context ( 'use_bottleneck' , False ):
677
675
result = nanops .nansem (arr , axis = 0 )
678
676
assert not (result < 0 ).any ()
679
- nanops ._USE_BOTTLENECK = True
680
677
681
678
def test_skew (self ):
682
679
tm ._skip_if_no_scipy ()
@@ -763,7 +760,7 @@ def wrapper(x):
763
760
tm .assert_series_equal (result0 , frame .apply (skipna_wrapper ),
764
761
check_dtype = check_dtype ,
765
762
check_less_precise = check_less_precise )
766
- if not tm . _incompat_bottleneck_version ( name ) :
763
+ if name in [ 'sum' , 'prod' ] :
767
764
exp = frame .apply (skipna_wrapper , axis = 1 )
768
765
tm .assert_series_equal (result1 , exp , check_dtype = False ,
769
766
check_less_precise = check_less_precise )
@@ -795,7 +792,7 @@ def wrapper(x):
795
792
all_na = self .frame * np .NaN
796
793
r0 = getattr (all_na , name )(axis = 0 )
797
794
r1 = getattr (all_na , name )(axis = 1 )
798
- if not tm . _incompat_bottleneck_version ( name ) :
795
+ if name in [ 'sum' , 'prod' ] :
799
796
assert np .isnan (r0 ).all ()
800
797
assert np .isnan (r1 ).all ()
801
798
@@ -1855,14 +1852,14 @@ def test_dataframe_clip(self):
1855
1852
assert (clipped_df .values [ub_mask ] == ub ).all ()
1856
1853
assert (clipped_df .values [mask ] == df .values [mask ]).all ()
1857
1854
1858
- @pytest .mark .xfail (reason = ("clip on mixed integer or floats "
1859
- "with integer clippers coerces to float" ))
1860
1855
def test_clip_mixed_numeric (self ):
1861
-
1856
+ # TODO(jreback)
1857
+ # clip on mixed integer or floats
1858
+ # with integer clippers coerces to float
1862
1859
df = DataFrame ({'A' : [1 , 2 , 3 ],
1863
1860
'B' : [1. , np .nan , 3. ]})
1864
1861
result = df .clip (1 , 2 )
1865
- expected = DataFrame ({'A' : [1 , 2 , 2 ],
1862
+ expected = DataFrame ({'A' : [1 , 2 , 2. ],
1866
1863
'B' : [1. , np .nan , 2. ]})
1867
1864
tm .assert_frame_equal (result , expected , check_like = True )
1868
1865
0 commit comments