@@ -575,24 +575,6 @@ def test_duplicate(self):
575
575
]
576
576
assert result == expected
577
577
578
- def test_format_with_na_rep (self ):
579
- # GH 21527 28358
580
- df = DataFrame ([[None , None ], [1.1 , 1.2 ]], columns = ["A" , "B" ])
581
-
582
- ctx = df .style .format (None , na_rep = "-" )._translate ()
583
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "-"
584
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "-"
585
-
586
- ctx = df .style .format ("{:.2%}" , na_rep = "-" )._translate ()
587
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "-"
588
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "-"
589
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "110.00%"
590
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "120.00%"
591
-
592
- ctx = df .style .format ("{:.2%}" , na_rep = "-" , subset = ["B" ])._translate ()
593
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "-"
594
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "120.00%"
595
-
596
578
def test_init_with_na_rep (self ):
597
579
# GH 21527 28358
598
580
df = DataFrame ([[None , None ], [1.1 , 1.2 ]], columns = ["A" , "B" ])
@@ -619,65 +601,6 @@ def test_set_na_rep(self):
619
601
assert ctx ["body" ][0 ][1 ]["display_value" ] == "NA"
620
602
assert ctx ["body" ][0 ][2 ]["display_value" ] == "-"
621
603
622
- def test_format_non_numeric_na (self ):
623
- # GH 21527 28358
624
- df = DataFrame (
625
- {
626
- "object" : [None , np .nan , "foo" ],
627
- "datetime" : [None , pd .NaT , pd .Timestamp ("20120101" )],
628
- }
629
- )
630
-
631
- with tm .assert_produces_warning (FutureWarning ):
632
- ctx = df .style .set_na_rep ("NA" )._translate ()
633
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "NA"
634
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "NA"
635
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "NA"
636
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "NA"
637
-
638
- ctx = df .style .format (None , na_rep = "-" )._translate ()
639
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "-"
640
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "-"
641
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "-"
642
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "-"
643
-
644
- def test_format_clear (self ):
645
- assert (0 , 0 ) not in self .styler ._display_funcs # using default
646
- self .styler .format ("{:.2f" )
647
- assert (0 , 0 ) in self .styler ._display_funcs # formatter is specified
648
- self .styler .format ()
649
- assert (0 , 0 ) not in self .styler ._display_funcs # formatter cleared to default
650
-
651
- def test_format_escape (self ):
652
- df = DataFrame ([['<>&"' ]])
653
- s = Styler (df , uuid_len = 0 ).format ("X&{0}>X" , escape = False )
654
- expected = '<td id="T__row0_col0" class="data row0 col0" >X&<>&">X</td>'
655
- assert expected in s .render ()
656
-
657
- # only the value should be escaped before passing to the formatter
658
- s = Styler (df , uuid_len = 0 ).format ("X&{0}>X" , escape = True )
659
- ex = '<td id="T__row0_col0" class="data row0 col0" >X&<>&">X</td>'
660
- assert ex in s .render ()
661
-
662
- def test_format_escape_na_rep (self ):
663
- # tests the na_rep is not escaped
664
- df = DataFrame ([['<>&"' , None ]])
665
- s = Styler (df , uuid_len = 0 ).format ("X&{0}>X" , escape = True , na_rep = "&" )
666
- ex = '<td id="T__row0_col0" class="data row0 col0" >X&<>&">X</td>'
667
- expected2 = '<td id="T__row0_col1" class="data row0 col1" >&</td>'
668
- assert ex in s .render ()
669
- assert expected2 in s .render ()
670
-
671
- def test_format_escape_floats (self ):
672
- # test given formatter for number format is not impacted by escape
673
- s = self .df .style .format ("{:.1f}" , escape = True )
674
- for expected in [">0.0<" , ">1.0<" , ">-1.2<" , ">-0.6<" ]:
675
- assert expected in s .render ()
676
- # tests precision of floats is not impacted by escape
677
- s = self .df .style .format (precision = 1 , escape = True )
678
- for expected in [">0<" , ">1<" , ">-1.2<" , ">-0.6<" ]:
679
- assert expected in s .render ()
680
-
681
604
def test_nonunique_raises (self ):
682
605
df = DataFrame ([[1 , 2 ]], columns = ["A" , "A" ])
683
606
msg = "style is not supported for non-unique indices."
@@ -804,85 +727,6 @@ def test_export(self):
804
727
assert style1 ._todo == style2 ._todo
805
728
style2 .render ()
806
729
807
- def test_display_format (self ):
808
- df = DataFrame (np .random .random (size = (2 , 2 )))
809
- ctx = df .style .format ("{:0.1f}" )._translate ()
810
-
811
- assert all (["display_value" in c for c in row ] for row in ctx ["body" ])
812
- assert all (
813
- [len (c ["display_value" ]) <= 3 for c in row [1 :]] for row in ctx ["body" ]
814
- )
815
- assert len (ctx ["body" ][0 ][1 ]["display_value" ].lstrip ("-" )) <= 3
816
-
817
- @pytest .mark .parametrize ("formatter" , [5 , True , [2.0 ]])
818
- def test_format_raises (self , formatter ):
819
- with pytest .raises (TypeError , match = "expected str or callable" ):
820
- self .df .style .format (formatter )
821
-
822
- def test_format_with_precision (self ):
823
- # Issue #13257
824
- df = DataFrame (data = [[1.0 , 2.0090 ], [3.2121 , 4.566 ]], columns = ["a" , "b" ])
825
- s = Styler (df )
826
-
827
- ctx = s .format (precision = 1 )._translate ()
828
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.0"
829
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.0"
830
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.2"
831
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.6"
832
-
833
- ctx = s .format (precision = 2 )._translate ()
834
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.00"
835
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.01"
836
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.21"
837
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.57"
838
-
839
- ctx = s .format (precision = 3 )._translate ()
840
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.000"
841
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.009"
842
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.212"
843
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.566"
844
-
845
- def test_format_subset (self ):
846
- df = DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
847
- ctx = df .style .format (
848
- {"a" : "{:0.1f}" , "b" : "{0:.2%}" }, subset = pd .IndexSlice [0 , :]
849
- )._translate ()
850
- expected = "0.1"
851
- raw_11 = "1.123400"
852
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
853
- assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
854
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "12.34%"
855
-
856
- ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , :])._translate ()
857
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
858
- assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
859
-
860
- ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice ["a" ])._translate ()
861
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
862
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.123400"
863
-
864
- ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , "a" ])._translate ()
865
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
866
- assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
867
-
868
- ctx = df .style .format (
869
- "{:0.1f}" , subset = pd .IndexSlice [[0 , 1 ], ["a" ]]
870
- )._translate ()
871
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
872
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "1.1"
873
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.123400"
874
- assert ctx ["body" ][1 ][2 ]["display_value" ] == raw_11
875
-
876
- def test_format_dict (self ):
877
- df = DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
878
- ctx = df .style .format ({"a" : "{:0.1f}" , "b" : "{0:.2%}" })._translate ()
879
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "0.1"
880
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "12.34%"
881
- df ["c" ] = ["aaa" , "bbb" ]
882
- ctx = df .style .format ({"a" : "{:0.1f}" , "c" : str .upper })._translate ()
883
- assert ctx ["body" ][0 ][1 ]["display_value" ] == "0.1"
884
- assert ctx ["body" ][0 ][3 ]["display_value" ] == "AAA"
885
-
886
730
def test_bad_apply_shape (self ):
887
731
df = DataFrame ([[1 , 2 ], [3 , 4 ]])
888
732
msg = "returned the wrong shape"
0 commit comments