@@ -605,8 +605,15 @@ def test_fillna_dict_series(self):
605
605
tm .assert_frame_equal (result , expected )
606
606
607
607
# disable this for now
608
- with pytest .raises (NotImplementedError , match = "column by column" ):
609
- df .fillna (df .max (1 ), axis = 1 )
608
+ expected = DataFrame (
609
+ {
610
+ "a" : [1.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
611
+ "b" : [1.0 , 2.0 , 3.0 , 3.0 , 4.0 ],
612
+ "c" : [1.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
613
+ }
614
+ )
615
+ result = df .fillna (df .max (1 ), axis = 1 )
616
+ tm .assert_frame_equal (expected , result )
610
617
611
618
def test_fillna_dataframe (self ):
612
619
# GH 8377
@@ -983,3 +990,85 @@ def test_interp_time_inplace_axis(self, axis):
983
990
result = expected .interpolate (axis = 0 , method = "time" )
984
991
expected .interpolate (axis = 0 , method = "time" , inplace = True )
985
992
tm .assert_frame_equal (result , expected )
993
+
994
+ @pytest .mark .parametrize (
995
+ "expected,fill_value" ,
996
+ [
997
+ (
998
+ DataFrame (
999
+ [[100 , 100 ], [200 , 4 ], [5 , 6 ]], columns = list ("AB" ), dtype = "float64"
1000
+ ),
1001
+ Series ([100 , 200 , 300 ]),
1002
+ ),
1003
+ (
1004
+ DataFrame (
1005
+ [[100 , 100 ], [np .nan , 4 ], [5 , 6 ]],
1006
+ columns = list ("AB" ),
1007
+ dtype = "float64" ,
1008
+ ),
1009
+ {0 : 100 , 2 : 300 , 3 : 400 },
1010
+ ),
1011
+ ],
1012
+ )
1013
+ def test_fillna_column_wise (self , expected , fill_value ):
1014
+ # GH 4514
1015
+ df = DataFrame ([[np .nan , np .nan ], [np .nan , 4 ], [5 , 6 ]], columns = list ("AB" ))
1016
+ result = df .fillna (fill_value , axis = 1 )
1017
+ tm .assert_frame_equal (expected , result )
1018
+
1019
+ df .fillna (fill_value , axis = 1 , inplace = True )
1020
+ tm .assert_frame_equal (expected , df )
1021
+
1022
+ def test_fillna_column_wise_downcast (self ):
1023
+ df = DataFrame ([[np .nan , 2 ], [3 , np .nan ], [np .nan , np .nan ]], columns = list ("AB" ))
1024
+ s = Series ([100 , 200 , 300 ])
1025
+
1026
+ expected = DataFrame (
1027
+ [[100 , 2 ], [3 , 200 ], [300 , 300 ]], columns = list ("AB" ), dtype = "int64"
1028
+ )
1029
+ result = df .fillna (s , axis = 1 , downcast = "infer" )
1030
+ tm .assert_frame_equal (expected , result )
1031
+
1032
+ def test_fillna_column_wise_duplicated_with_series_dict (self ):
1033
+ # GH 4514
1034
+ df = DataFrame (
1035
+ [[np .nan , np .nan , 3 ], [np .nan , 5 , np .nan ], [7 , np .nan , np .nan ]],
1036
+ columns = list ("ABB" ),
1037
+ index = [0 , 0 , 1 ],
1038
+ )
1039
+ expected = DataFrame (
1040
+ [[100 , 100 , 3 ], [100 , 5 , 100 ], [7 , 200 , 200 ]],
1041
+ columns = list ("ABB" ),
1042
+ index = [0 , 0 , 1 ],
1043
+ dtype = "float64" ,
1044
+ )
1045
+ s = pd .Series ([100 , 200 , 300 ], index = [0 , 1 , 2 ])
1046
+ d = {0 : 100 , 1 : 200 , 2 : 300 }
1047
+
1048
+ result = df .fillna (s , axis = 1 )
1049
+ tm .assert_frame_equal (result , expected )
1050
+
1051
+ result = df .fillna (d , axis = 1 )
1052
+ tm .assert_frame_equal (result , expected )
1053
+
1054
+ def test_fillna_duplicated_with_series_dict (self ):
1055
+ # GH 4514
1056
+ df = DataFrame (
1057
+ [[np .nan , np .nan , 3 ], [np .nan , 5 , np .nan ], [7 , np .nan , np .nan ]],
1058
+ columns = list ("ABB" ),
1059
+ index = [0 , 0 , 1 ],
1060
+ )
1061
+ expected = DataFrame (
1062
+ [[100 , 200 , 3 ], [100 , 5 , 200 ], [7 , 200 , 200 ]],
1063
+ columns = list ("ABB" ),
1064
+ index = [0 , 0 , 1 ],
1065
+ dtype = "float64" ,
1066
+ )
1067
+ s = pd .Series ([100 , 200 , 300 ], index = ["A" , "B" , "C" ])
1068
+ d = {"A" : 100 , "B" : 200 , "C" : 300 }
1069
+
1070
+ result = df .fillna (s )
1071
+ tm .assert_frame_equal (result , expected )
1072
+
1073
+ result = df .fillna (d )
1074
+ tm .assert_frame_equal (result , expected )
0 commit comments