25
25
26
26
from pandas ._config import get_option
27
27
28
+ from pandas ._typing import Axis , FrameOrSeries
28
29
from pandas .compat ._optional import import_optional_dependency
29
30
from pandas .util ._decorators import Appender
30
31
31
32
from pandas .core .dtypes .common import is_float
32
33
33
34
import pandas as pd
34
- from pandas ._typing import Axis , FrameOrSeries
35
35
from pandas .api .types import is_dict_like , is_list_like
36
36
import pandas .core .common as com
37
37
from pandas .core .generic import _shared_docs
51
51
52
52
53
53
@contextmanager
54
- def _mpl (func ):
54
+ def _mpl (func : Callable ):
55
55
if has_mpl :
56
56
yield plt , colors
57
57
else :
@@ -135,9 +135,9 @@ class Styler:
135
135
136
136
def __init__ (
137
137
self ,
138
- data ,
138
+ data : FrameOrSeries ,
139
139
precision : Optional [int ] = None ,
140
- table_styles = None ,
140
+ table_styles : Union [ List [ Dict [ str , List [ Tuple [ str , str ]]]], None ] = None ,
141
141
uuid : Optional [str ] = None ,
142
142
caption : Optional [str ] = None ,
143
143
table_attributes : Optional [str ] = None ,
@@ -185,7 +185,7 @@ def default_display_func(x):
185
185
Tuple [int , int ], Callable [[Any ], str ]
186
186
] = defaultdict (lambda : default_display_func )
187
187
188
- def _repr_html_ (self ):
188
+ def _repr_html_ (self ) -> str :
189
189
"""
190
190
Hooks into Jupyter notebook rich display system.
191
191
"""
@@ -221,7 +221,7 @@ def to_excel(
221
221
inf_rep : str = "inf" ,
222
222
verbose : bool = True ,
223
223
freeze_panes : Optional [Tuple [int , int ]] = None ,
224
- ):
224
+ ) -> None :
225
225
226
226
from pandas .io .formats .excel import ExcelFormatter
227
227
@@ -245,7 +245,7 @@ def to_excel(
245
245
engine = engine ,
246
246
)
247
247
248
- def _translate (self ):
248
+ def _translate (self ) -> Dict :
249
249
"""
250
250
Convert the DataFrame in `self.data` and the attrs from `_build_styles`
251
251
into a dictionary of {head, body, uuid, cellstyle}.
@@ -265,14 +265,14 @@ def _translate(self):
265
265
BLANK_CLASS = "blank"
266
266
BLANK_VALUE = ""
267
267
268
- def format_attr (pair ) -> str :
268
+ def format_attr (pair : Dict [ str , str ] ) -> str :
269
269
return "{key}={value}" .format (** pair )
270
270
271
271
# for sparsifying a MultiIndex
272
272
idx_lengths = _get_level_lengths (self .index )
273
273
col_lengths = _get_level_lengths (self .columns , hidden_columns )
274
274
275
- cell_context = dict ()
275
+ cell_context : Dict = dict ()
276
276
277
277
n_rlvls = self .data .index .nlevels
278
278
n_clvls = self .data .columns .nlevels
@@ -555,16 +555,18 @@ def render(self, **kwargs) -> str:
555
555
d .update (kwargs )
556
556
return self .template .render (** d )
557
557
558
- def _update_ctx (self , attrs ) -> None :
558
+ def _update_ctx (self , attrs : FrameOrSeries ) -> None :
559
559
"""
560
560
Update the state of the Styler.
561
561
562
562
Collects a mapping of {index_label: ['<property>: <value>']}.
563
563
564
+ Parameters
565
+ ----------
564
566
attrs : Series or DataFrame
565
- should contain strings of '<property>: <value>;<prop2>: <val2>'
566
- Whitespace shouldn't matter and the final trailing ';' shouldn't
567
- matter.
567
+ should contain strings of '<property>: <value>;<prop2>: <val2>'
568
+ Whitespace shouldn't matter and the final trailing ';' shouldn't
569
+ matter.
568
570
"""
569
571
for row_label , v in attrs .iterrows ():
570
572
for col_label , col in v .items ():
@@ -573,7 +575,7 @@ def _update_ctx(self, attrs) -> None:
573
575
for pair in col .rstrip (";" ).split (";" ):
574
576
self .ctx [(i , j )].append (pair )
575
577
576
- def _copy (self , deepcopy : bool = False ):
578
+ def _copy (self , deepcopy : bool = False ) -> "Styler" :
577
579
styler = Styler (
578
580
self .data ,
579
581
precision = self .precision ,
@@ -590,16 +592,16 @@ def _copy(self, deepcopy: bool = False):
590
592
styler ._todo = self ._todo
591
593
return styler
592
594
593
- def __copy__ (self ):
595
+ def __copy__ (self ) -> "Styler" :
594
596
"""
595
597
Deep copy by default.
596
598
"""
597
599
return self ._copy (deepcopy = False )
598
600
599
- def __deepcopy__ (self , memo ):
601
+ def __deepcopy__ (self , memo ) -> "Styler" :
600
602
return self ._copy (deepcopy = True )
601
603
602
- def clear (self ):
604
+ def clear (self ) -> None :
603
605
"""
604
606
Reset the styler, removing any previously applied styles.
605
607
@@ -622,7 +624,9 @@ def _compute(self):
622
624
r = func (self )(* args , ** kwargs )
623
625
return r
624
626
625
- def _apply (self , func , axis : Optional [Axis ] = 0 , subset = None , ** kwargs ) -> "Styler" :
627
+ def _apply (
628
+ self , func : Callable , axis : Optional [Axis ] = 0 , subset = None , ** kwargs
629
+ ) -> "Styler" :
626
630
subset = slice (None ) if subset is None else subset
627
631
subset = _non_reducing_slice (subset )
628
632
data = self .data .loc [subset ]
@@ -655,7 +659,9 @@ def _apply(self, func, axis: Optional[Axis] = 0, subset=None, **kwargs) -> "Styl
655
659
self ._update_ctx (result )
656
660
return self
657
661
658
- def apply (self , func , axis : Optional [Axis ] = 0 , subset = None , ** kwargs ) -> "Styler" :
662
+ def apply (
663
+ self , func : Callable , axis : Optional [Axis ] = 0 , subset = None , ** kwargs
664
+ ) -> "Styler" :
659
665
"""
660
666
Apply a function column-wise, row-wise, or table-wise.
661
667
@@ -706,7 +712,7 @@ def apply(self, func, axis: Optional[Axis] = 0, subset=None, **kwargs) -> "Style
706
712
)
707
713
return self
708
714
709
- def _applymap (self , func , subset = None , ** kwargs ) -> "Styler" :
715
+ def _applymap (self , func : Callable , subset = None , ** kwargs ) -> "Styler" :
710
716
func = partial (func , ** kwargs ) # applymap doesn't take kwargs?
711
717
if subset is None :
712
718
subset = pd .IndexSlice [:]
@@ -715,7 +721,7 @@ def _applymap(self, func, subset=None, **kwargs) -> "Styler":
715
721
self ._update_ctx (result )
716
722
return self
717
723
718
- def applymap (self , func , subset = None , ** kwargs ) -> "Styler" :
724
+ def applymap (self , func : Callable , subset = None , ** kwargs ) -> "Styler" :
719
725
"""
720
726
Apply a function elementwise.
721
727
@@ -745,7 +751,12 @@ def applymap(self, func, subset=None, **kwargs) -> "Styler":
745
751
return self
746
752
747
753
def where (
748
- self , cond , value : str , other : Optional [str ] = None , subset = None , ** kwargs
754
+ self ,
755
+ cond : Callable ,
756
+ value : str ,
757
+ other : Optional [str ] = None ,
758
+ subset = None ,
759
+ ** kwargs ,
749
760
) -> "Styler" :
750
761
"""
751
762
Apply a function elementwise.
@@ -823,7 +834,7 @@ def set_table_attributes(self, attributes: str) -> "Styler":
823
834
self .table_attributes = attributes
824
835
return self
825
836
826
- def export (self ) -> List :
837
+ def export (self ) -> List [ Tuple [ Callable , Tuple , Dict ]] :
827
838
"""
828
839
Export the styles to applied to the current Styler.
829
840
@@ -839,7 +850,7 @@ def export(self) -> List:
839
850
"""
840
851
return self ._todo
841
852
842
- def use (self , styles : List ) -> "Styler" :
853
+ def use (self , styles : List [ Tuple [ Callable , Tuple , Dict ]] ) -> "Styler" :
843
854
"""
844
855
Set the styles on the current Styler.
845
856
@@ -891,7 +902,9 @@ def set_caption(self, caption: str) -> "Styler":
891
902
self .caption = caption
892
903
return self
893
904
894
- def set_table_styles (self , table_styles : List ) -> "Styler" :
905
+ def set_table_styles (
906
+ self , table_styles : List [Dict [str , List [Tuple [str , str ]]]]
907
+ ) -> "Styler" :
895
908
"""
896
909
Set the table styles on a Styler.
897
910
@@ -1170,8 +1183,8 @@ def set_properties(self, subset=None, **kwargs) -> "Styler":
1170
1183
def _bar (
1171
1184
s ,
1172
1185
align : str ,
1173
- colors ,
1174
- width = 100 ,
1186
+ colors : List [ str ] ,
1187
+ width : float = 100 ,
1175
1188
vmin : Optional [float ] = None ,
1176
1189
vmax : Optional [float ] = None ,
1177
1190
):
@@ -1192,7 +1205,7 @@ def _bar(
1192
1205
normed = width * (s .to_numpy (dtype = float ) - smin ) / (smax - smin + 1e-12 )
1193
1206
zero = - width * smin / (smax - smin + 1e-12 )
1194
1207
1195
- def css_bar (start , end , color ) :
1208
+ def css_bar (start : float , end : float , color : str ) -> str :
1196
1209
"""
1197
1210
Generate CSS code to draw a bar from start to end.
1198
1211
"""
@@ -1417,7 +1430,7 @@ class MyStyler(cls):
1417
1430
1418
1431
return MyStyler
1419
1432
1420
- def pipe (self , func , * args , ** kwargs ):
1433
+ def pipe (self , func : Callable , * args , ** kwargs ):
1421
1434
"""
1422
1435
Apply ``func(self, *args, **kwargs)``, and return the result.
1423
1436
@@ -1540,7 +1553,9 @@ def _get_level_lengths(index, hidden_elements=None):
1540
1553
return non_zero_lengths
1541
1554
1542
1555
1543
- def _maybe_wrap_formatter (formatter , na_rep : Optional [str ]):
1556
+ def _maybe_wrap_formatter (
1557
+ formatter : Union [Callable , str ], na_rep : Optional [str ]
1558
+ ) -> Callable :
1544
1559
if isinstance (formatter , str ):
1545
1560
formatter_func = lambda x : formatter .format (x )
1546
1561
elif callable (formatter ):
0 commit comments