@@ -755,40 +755,37 @@ def test_replace_for_new_dtypes(self):
755755 result = tsframe .fillna (method = 'bfill' )
756756 assert_frame_equal (result , tsframe .fillna (method = 'bfill' ))
757757
758- def test_replace_dtypes (self ):
759- # int
760- df = DataFrame ({'ints' : [1 , 2 , 3 ]})
761- result = df .replace (1 , 0 )
762- expected = DataFrame ({'ints' : [0 , 2 , 3 ]})
763- assert_frame_equal (result , expected )
764-
765- df = DataFrame ({'ints' : [1 , 2 , 3 ]}, dtype = np .int32 )
766- result = df .replace (1 , 0 )
767- expected = DataFrame ({'ints' : [0 , 2 , 3 ]}, dtype = np .int32 )
768- assert_frame_equal (result , expected )
769-
770- df = DataFrame ({'ints' : [1 , 2 , 3 ]}, dtype = np .int16 )
771- result = df .replace (1 , 0 )
772- expected = DataFrame ({'ints' : [0 , 2 , 3 ]}, dtype = np .int16 )
773- assert_frame_equal (result , expected )
774-
775- # bools
776- df = DataFrame ({'bools' : [True , False , True ]})
777- result = df .replace (False , True )
778- assert result .values .all ()
779-
780- # complex blocks
781- df = DataFrame ({'complex' : [1j , 2j , 3j ]})
782- result = df .replace (1j , 0j )
783- expected = DataFrame ({'complex' : [0j , 2j , 3j ]})
784- assert_frame_equal (result , expected )
785-
786- # datetime blocks
787- prev = datetime .today ()
788- now = datetime .today ()
789- df = DataFrame ({'datetime64' : Index ([prev , now , prev ])})
790- result = df .replace (prev , now )
791- expected = DataFrame ({'datetime64' : Index ([now ] * 3 )})
758+ @pytest .mark .parametrize ('frame, to_replace, value, expected' , [
759+ (DataFrame ({'ints' : [1 , 2 , 3 ]}), 1 , 0 ,
760+ DataFrame ({'ints' : [0 , 2 , 3 ]})),
761+ (DataFrame ({'ints' : [1 , 2 , 3 ]}, dtype = np .int32 ), 1 , 0 ,
762+ DataFrame ({'ints' : [0 , 2 , 3 ]}, dtype = np .int32 )),
763+ (DataFrame ({'ints' : [1 , 2 , 3 ]}, dtype = np .int16 ), 1 , 0 ,
764+ DataFrame ({'ints' : [0 , 2 , 3 ]}, dtype = np .int16 )),
765+ (DataFrame ({'bools' : [True , False , True ]}), False , True ,
766+ DataFrame ({'bools' : [True , True , True ]})),
767+ (DataFrame ({'complex' : [1j , 2j , 3j ]}), 1j , 0 ,
768+ DataFrame ({'complex' : [0j , 2j , 3j ]})),
769+ (DataFrame ({'datetime64' : Index ([datetime (2018 , 5 , 28 ),
770+ datetime (2018 , 7 , 28 ),
771+ datetime (2018 , 5 , 28 )])}),
772+ datetime (2018 , 5 , 28 ), datetime (2018 , 7 , 28 ),
773+ DataFrame ({'datetime64' : Index ([datetime (2018 , 7 , 28 )] * 3 )})),
774+ # GH 20380
775+ (DataFrame ({'dt' : [datetime (3017 , 12 , 20 )], 'str' : ['foo' ]}),
776+ 'foo' , 'bar' ,
777+ DataFrame ({'dt' : [datetime (3017 , 12 , 20 )], 'str' : ['bar' ]})),
778+ (DataFrame ({'A' : date_range ('20130101' , periods = 3 , tz = 'US/Eastern' ),
779+ 'B' : [0 , np .nan , 2 ]}),
780+ Timestamp ('20130102' , tz = 'US/Eastern' ),
781+ Timestamp ('20130104' , tz = 'US/Eastern' ),
782+ DataFrame ({'A' : [Timestamp ('20130101' , tz = 'US/Eastern' ),
783+ Timestamp ('20130104' , tz = 'US/Eastern' ),
784+ Timestamp ('20130103' , tz = 'US/Eastern' )],
785+ 'B' : [0 , np .nan , 2 ]}))
786+ ])
787+ def test_replace_dtypes (self , frame , to_replace , value , expected ):
788+ result = getattr (frame , 'replace' )(to_replace , value )
792789 assert_frame_equal (result , expected )
793790
794791 def test_replace_input_formats_listlike (self ):
0 commit comments