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