@@ -602,9 +602,15 @@ def test_fillna_dict_series(self):
602
602
expected = df .fillna (df .max ().to_dict ())
603
603
tm .assert_frame_equal (result , expected )
604
604
605
- # disable this for now
606
- with pytest .raises (NotImplementedError , match = "column by column" ):
607
- df .fillna (df .max (1 ), axis = 1 )
605
+ expected = DataFrame (
606
+ {
607
+ "a" : [1.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
608
+ "b" : [1.0 , 2.0 , 3.0 , 3.0 , 4.0 ],
609
+ "c" : [1.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
610
+ }
611
+ )
612
+ result = df .fillna (df .max (1 ), axis = 1 )
613
+ tm .assert_frame_equal (expected , result )
608
614
609
615
def test_fillna_dataframe (self ):
610
616
# GH 8377
@@ -703,3 +709,86 @@ def test_fill_value_when_combine_const(self):
703
709
exp = df .fillna (0 ).add (2 )
704
710
res = df .add (2 , fill_value = 0 )
705
711
tm .assert_frame_equal (res , exp )
712
+
713
+ @pytest .mark .parametrize (
714
+ "expected,fill_value" ,
715
+ [
716
+ (
717
+ DataFrame (
718
+ [[100 , 100 ], [200 , 4 ], [5 , 6 ]], columns = list ("AB" ), dtype = "float64"
719
+ ),
720
+ Series ([100 , 200 , 300 ]),
721
+ ),
722
+ (
723
+ DataFrame (
724
+ [[100 , 100 ], [np .nan , 4 ], [5 , 6 ]],
725
+ columns = list ("AB" ),
726
+ dtype = "float64" ,
727
+ ),
728
+ {0 : 100 , 2 : 300 , 3 : 400 },
729
+ ),
730
+ ],
731
+ )
732
+ def test_fillna_column_wise (self , expected , fill_value ):
733
+ # GH 4514
734
+ df = DataFrame ([[np .nan , np .nan ], [np .nan , 4 ], [5 , 6 ]], columns = list ("AB" ))
735
+ result = df .fillna (fill_value , axis = 1 )
736
+ tm .assert_frame_equal (expected , result )
737
+
738
+ df .fillna (fill_value , axis = 1 , inplace = True )
739
+ tm .assert_frame_equal (expected , df )
740
+
741
+ def test_fillna_column_wise_downcast (self ):
742
+ df = DataFrame ([[np .nan , 2 ], [3 , np .nan ], [np .nan , np .nan ]], columns = list ("AB" ))
743
+ s = Series ([100 , 200 , 300 ])
744
+
745
+ expected = DataFrame (
746
+ [[100 , 2 ], [3 , 200 ], [300 , 300 ]], columns = list ("AB" ), dtype = "int64"
747
+ )
748
+ result = df .fillna (s , axis = 1 , downcast = "infer" )
749
+ tm .assert_frame_equal (expected , result )
750
+
751
+ @pytest .mark .parametrize (
752
+ "fill_value" ,
753
+ [Series ([100 , 200 , 300 ], index = [0 , 1 , 2 ]), {0 : 100 , 1 : 200 , 2 : 300 }],
754
+ )
755
+ def test_fillna_column_wise_duplicated_with_series_dict (self , fill_value ):
756
+ # GH 4514
757
+ df = DataFrame (
758
+ [[np .nan , np .nan , 3 ], [np .nan , 5 , np .nan ], [7 , np .nan , np .nan ]],
759
+ columns = list ("ABB" ),
760
+ index = [0 , 0 , 1 ],
761
+ )
762
+ expected = DataFrame (
763
+ [[100 , 100 , 3 ], [100 , 5 , 100 ], [7 , 200 , 200 ]],
764
+ columns = list ("ABB" ),
765
+ index = [0 , 0 , 1 ],
766
+ dtype = "float64" ,
767
+ )
768
+
769
+ result = df .fillna (fill_value , axis = 1 )
770
+ tm .assert_frame_equal (result , expected )
771
+
772
+ @pytest .mark .parametrize (
773
+ "fill_value" ,
774
+ [
775
+ Series ([100 , 200 , 300 ], index = ["A" , "B" , "C" ]),
776
+ {"A" : 100 , "B" : 200 , "C" : 300 },
777
+ ],
778
+ )
779
+ def test_fillna_duplicated_with_series_dict (self , fill_value ):
780
+ # GH 4514
781
+ df = DataFrame (
782
+ [[np .nan , np .nan , 3 ], [np .nan , 5 , np .nan ], [7 , np .nan , np .nan ]],
783
+ columns = list ("ABB" ),
784
+ index = [0 , 0 , 1 ],
785
+ )
786
+ expected = DataFrame (
787
+ [[100 , 200 , 3 ], [100 , 5 , 200 ], [7 , 200 , 200 ]],
788
+ columns = list ("ABB" ),
789
+ index = [0 , 0 , 1 ],
790
+ dtype = "float64" ,
791
+ )
792
+
793
+ result = df .fillna (fill_value )
794
+ tm .assert_frame_equal (result , expected )
0 commit comments