@@ -459,7 +459,10 @@ def test_interpolate(self):
459459 self .assert_numpy_array_equal (time_interp , ord_ts )
460460
461461 # try time interpolation on a non-TimeSeries
462- self .assertRaises (ValueError , self .series .interpolate , method = 'time' )
462+ # Only raises ValueError if there are NaNs.
463+ non_ts = self .series .copy ()
464+ non_ts [0 ] = np .NaN
465+ self .assertRaises (ValueError , non_ts .interpolate , method = 'time' )
463466
464467 def test_interp_regression (self ):
465468 _skip_if_no_scipy ()
@@ -512,7 +515,7 @@ def test_interpolate_non_ts(self):
512515 def test_nan_interpolate (self ):
513516 s = Series ([0 , 1 , np .nan , 3 ])
514517 result = s .interpolate ()
515- expected = Series ([0 , 1 , 2 , 3 ])
518+ expected = Series ([0. , 1. , 2. , 3. ])
516519 assert_series_equal (result , expected )
517520
518521 _skip_if_no_scipy ()
@@ -522,20 +525,20 @@ def test_nan_interpolate(self):
522525 def test_nan_irregular_index (self ):
523526 s = Series ([1 , 2 , np .nan , 4 ], index = [1 , 3 , 5 , 9 ])
524527 result = s .interpolate ()
525- expected = Series ([1 , 2 , 3 , 4 ], index = [1 , 3 , 5 , 9 ])
528+ expected = Series ([1. , 2. , 3. , 4. ], index = [1 , 3 , 5 , 9 ])
526529 assert_series_equal (result , expected )
527530
528531 def test_nan_str_index (self ):
529532 s = Series ([0 , 1 , 2 , np .nan ], index = list ('abcd' ))
530533 result = s .interpolate ()
531- expected = Series ([0 , 1 , 2 , 2 ], index = list ('abcd' ))
534+ expected = Series ([0. , 1. , 2. , 2. ], index = list ('abcd' ))
532535 assert_series_equal (result , expected )
533536
534537 def test_interp_quad (self ):
535538 _skip_if_no_scipy ()
536539 sq = Series ([1 , 4 , np .nan , 16 ], index = [1 , 2 , 3 , 4 ])
537540 result = sq .interpolate (method = 'quadratic' )
538- expected = Series ([1 , 4 , 9 , 16 ], index = [1 , 2 , 3 , 4 ])
541+ expected = Series ([1. , 4. , 9. , 16. ], index = [1 , 2 , 3 , 4 ])
539542 assert_series_equal (result , expected )
540543
541544 def test_interp_scipy_basic (self ):
@@ -545,18 +548,30 @@ def test_interp_scipy_basic(self):
545548 expected = Series ([1. , 3. , 7.5 , 12. , 18.5 , 25. ])
546549 result = s .interpolate (method = 'slinear' )
547550 assert_series_equal (result , expected )
551+
552+ result = s .interpolate (method = 'slinear' , donwcast = 'infer' )
553+ assert_series_equal (result , expected )
548554 # nearest
549555 expected = Series ([1 , 3 , 3 , 12 , 12 , 25 ])
550556 result = s .interpolate (method = 'nearest' )
557+ assert_series_equal (result , expected .astype ('float' ))
558+
559+ result = s .interpolate (method = 'nearest' , downcast = 'infer' )
551560 assert_series_equal (result , expected )
552561 # zero
553562 expected = Series ([1 , 3 , 3 , 12 , 12 , 25 ])
554563 result = s .interpolate (method = 'zero' )
564+ assert_series_equal (result , expected .astype ('float' ))
565+
566+ result = s .interpolate (method = 'zero' , downcast = 'infer' )
555567 assert_series_equal (result , expected )
556568 # quadratic
557569 expected = Series ([1 , 3. , 6.769231 , 12. , 18.230769 , 25. ])
558570 result = s .interpolate (method = 'quadratic' )
559571 assert_series_equal (result , expected )
572+
573+ result = s .interpolate (method = 'quadratic' , downcast = 'infer' )
574+ assert_series_equal (result , expected )
560575 # cubic
561576 expected = Series ([1. , 3. , 6.8 , 12. , 18.2 , 25. ])
562577 result = s .interpolate (method = 'cubic' )
@@ -585,7 +600,6 @@ def test_interp_multiIndex(self):
585600
586601 expected = s .copy ()
587602 expected .loc [2 ] = 2
588- expected = expected .astype (np .int64 )
589603 result = s .interpolate ()
590604 assert_series_equal (result , expected )
591605
@@ -595,15 +609,15 @@ def test_interp_multiIndex(self):
595609
596610 def test_interp_nonmono_raise (self ):
597611 _skip_if_no_scipy ()
598- s = pd . Series ([1 , 2 , 3 ], index = [0 , 2 , 1 ])
612+ s = Series ([1 , np . nan , 3 ], index = [0 , 2 , 1 ])
599613 with tm .assertRaises (ValueError ):
600614 s .interpolate (method = 'krogh' )
601615
602616 def test_interp_datetime64 (self ):
603617 _skip_if_no_scipy ()
604618 df = Series ([1 , np .nan , 3 ], index = date_range ('1/1/2000' , periods = 3 ))
605619 result = df .interpolate (method = 'nearest' )
606- expected = Series ([1 , 1 , 3 ], index = date_range ('1/1/2000' , periods = 3 ))
620+ expected = Series ([1. , 1. , 3. ], index = date_range ('1/1/2000' , periods = 3 ))
607621 assert_series_equal (result , expected )
608622
609623class TestDataFrame (tm .TestCase , Generic ):
@@ -639,7 +653,7 @@ def test_get_numeric_data_preserve_dtype(self):
639653 def test_interp_basic (self ):
640654 df = DataFrame ({'A' : [1 , 2 , np .nan , 4 ], 'B' : [1 , 4 , 9 , np .nan ],
641655 'C' : [1 , 2 , 3 , 5 ], 'D' : list ('abcd' )})
642- expected = DataFrame ({'A' : [1 , 2 , 3 , 4 ], 'B' : [1 , 4 , 9 , 9 ],
656+ expected = DataFrame ({'A' : [1. , 2. , 3. , 4. ], 'B' : [1. , 4. , 9. , 9. ],
643657 'C' : [1 , 2 , 3 , 5 ], 'D' : list ('abcd' )})
644658 result = df .interpolate ()
645659 assert_frame_equal (result , expected )
@@ -648,8 +662,6 @@ def test_interp_basic(self):
648662 expected = df .set_index ('C' )
649663 expected .A .loc [3 ] = 3
650664 expected .B .loc [5 ] = 9
651- expected [['A' , 'B' ]] = expected [['A' , 'B' ]].astype (np .int64 )
652-
653665 assert_frame_equal (result , expected )
654666
655667 def test_interp_bad_method (self ):
@@ -663,9 +675,14 @@ def test_interp_combo(self):
663675 'C' : [1 , 2 , 3 , 5 ], 'D' : list ('abcd' )})
664676
665677 result = df ['A' ].interpolate ()
678+ expected = Series ([1. , 2. , 3. , 4. ])
679+ assert_series_equal (result , expected )
680+
681+ result = df ['A' ].interpolate (downcast = 'infer' )
666682 expected = Series ([1 , 2 , 3 , 4 ])
667683 assert_series_equal (result , expected )
668684
685+
669686 def test_interp_nan_idx (self ):
670687 df = DataFrame ({'A' : [1 , 2 , np .nan , 4 ], 'B' : [np .nan , 2 , 3 , 4 ]})
671688 df = df .set_index ('A' )
@@ -722,13 +739,16 @@ def test_interp_alt_scipy(self):
722739 expected = df .copy ()
723740 expected ['A' ].iloc [2 ] = 3
724741 expected ['A' ].iloc [5 ] = 6
742+ assert_frame_equal (result , expected )
743+
744+ result = df .interpolate (method = 'barycentric' , downcast = 'infer' )
725745 assert_frame_equal (result , expected .astype (np .int64 ))
726746
727747 result = df .interpolate (method = 'krogh' )
728748 expectedk = df .copy ()
729- expectedk ['A' ].iloc [2 ] = 3
730- expectedk ['A' ].iloc [5 ] = 6
731- expectedk ['A' ] = expected ['A' ]. astype ( np . int64 )
749+ # expectedk['A'].iloc[2] = 3
750+ # expectedk['A'].iloc[5] = 6
751+ expectedk ['A' ] = expected ['A' ]
732752 assert_frame_equal (result , expectedk )
733753
734754 _skip_if_no_pchip ()
@@ -786,9 +806,32 @@ def test_interp_raise_on_only_mixed(self):
786806
787807 def test_interp_inplace (self ):
788808 df = DataFrame ({'a' : [1. , 2. , np .nan , 4. ]})
789- expected = DataFrame ({'a' : [1 , 2 , 3 , 4 ]})
790- df ['a' ].interpolate (inplace = True )
791- assert_frame_equal (df , expected )
809+ expected = DataFrame ({'a' : [1. , 2. , 3. , 4. ]})
810+ result = df .copy ()
811+ result ['a' ].interpolate (inplace = True )
812+ assert_frame_equal (result , expected )
813+
814+ result = df .copy ()
815+ result ['a' ].interpolate (inplace = True , downcast = 'infer' )
816+ assert_frame_equal (result , expected .astype ('int' ))
817+
818+ def test_interp_ignore_all_good (self ):
819+ # GH
820+ df = DataFrame ({'A' : [1 , 2 , np .nan , 4 ],
821+ 'B' : [1 , 2 , 3 , 4 ],
822+ 'C' : [1. , 2. , np .nan , 4. ],
823+ 'D' : [1. , 2. , 3. , 4. ]})
824+ expected = DataFrame ({'A' : np .array ([1 , 2 , 3 , 4 ], dtype = 'float' ),
825+ 'B' : np .array ([1 , 2 , 3 , 4 ], dtype = 'int' ),
826+ 'C' : np .array ([1. , 2. , 3 , 4. ], dtype = 'float' ),
827+ 'D' : np .array ([1. , 2. , 3. , 4. ], dtype = 'float' )})
828+
829+ result = df .interpolate (downcast = None )
830+ assert_frame_equal (result , expected )
831+
832+ # all good
833+ result = df [['B' , 'D' ]].interpolate (downcast = None )
834+ assert_frame_equal (result , df [['B' , 'D' ]])
792835
793836 def test_no_order (self ):
794837 _skip_if_no_scipy ()
@@ -802,7 +845,7 @@ def test_spline(self):
802845 _skip_if_no_scipy ()
803846 s = Series ([1 , 2 , np .nan , 4 , 5 , np .nan , 7 ])
804847 result = s .interpolate (method = 'spline' , order = 1 )
805- expected = Series ([1 , 2 , 3 , 4 , 5 , 6 , 7 ])
848+ expected = Series ([1. , 2. , 3. , 4. , 5. , 6. , 7. ])
806849 assert_series_equal (result , expected )
807850
808851 def test_metadata_propagation_indiv (self ):
0 commit comments