Skip to content

Commit fca53a8

Browse files
vaibhavhrtproost
authored andcommitted
Annotate DataFrame Part 2 (Rendering Methods) (pandas-dev#28453)
1 parent c80af01 commit fca53a8

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

pandas/core/frame.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import itertools
1515
import sys
1616
from textwrap import dedent
17-
from typing import FrozenSet, List, Optional, Set, Tuple, Type, Union
17+
from typing import FrozenSet, List, Optional, Sequence, Set, Tuple, Type, Union
1818
import warnings
1919

2020
import numpy as np
@@ -79,7 +79,7 @@
7979
)
8080
from pandas.core.dtypes.missing import isna, notna
8181

82-
from pandas._typing import Axes, Dtype
82+
from pandas._typing import Axes, Dtype, FilePathOrBuffer
8383
from pandas.core import algorithms, common as com, nanops, ops
8484
from pandas.core.accessor import CachedAccessor
8585
from pandas.core.arrays import Categorical, ExtensionArray
@@ -558,14 +558,14 @@ def _is_homogeneous_type(self) -> bool:
558558
# ----------------------------------------------------------------------
559559
# Rendering Methods
560560

561-
def _repr_fits_vertical_(self):
561+
def _repr_fits_vertical_(self) -> bool:
562562
"""
563563
Check length against max_rows.
564564
"""
565565
max_rows = get_option("display.max_rows")
566566
return len(self) <= max_rows
567567

568-
def _repr_fits_horizontal_(self, ignore_width=False):
568+
def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
569569
"""
570570
Check if full repr fits in horizontal boundaries imposed by the display
571571
options width and max_columns.
@@ -619,7 +619,7 @@ def _repr_fits_horizontal_(self, ignore_width=False):
619619

620620
return repr_width < width
621621

622-
def _info_repr(self):
622+
def _info_repr(self) -> bool:
623623
"""
624624
True if the repr should show the info view.
625625
"""
@@ -628,7 +628,7 @@ def _info_repr(self):
628628
self._repr_fits_horizontal_() and self._repr_fits_vertical_()
629629
)
630630

631-
def __repr__(self):
631+
def __repr__(self) -> str:
632632
"""
633633
Return a string representation for a particular DataFrame.
634634
"""
@@ -658,7 +658,7 @@ def __repr__(self):
658658

659659
return buf.getvalue()
660660

661-
def _repr_html_(self):
661+
def _repr_html_(self) -> Optional[str]:
662662
"""
663663
Return a html representation for a particular DataFrame.
664664
@@ -705,6 +705,7 @@ def _repr_html_(self):
705705
return None
706706

707707
@Substitution(
708+
header_type="bool or sequence",
708709
header="Write out the column names. If a list of strings "
709710
"is given, it is assumed to be aliases for the "
710711
"column names",
@@ -714,25 +715,25 @@ def _repr_html_(self):
714715
@Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring)
715716
def to_string(
716717
self,
717-
buf=None,
718-
columns=None,
719-
col_space=None,
720-
header=True,
721-
index=True,
722-
na_rep="NaN",
723-
formatters=None,
724-
float_format=None,
725-
sparsify=None,
726-
index_names=True,
727-
justify=None,
728-
max_rows=None,
729-
min_rows=None,
730-
max_cols=None,
731-
show_dimensions=False,
732-
decimal=".",
733-
line_width=None,
734-
max_colwidth=None,
735-
):
718+
buf: Optional[FilePathOrBuffer[str]] = None,
719+
columns: Optional[Sequence[str]] = None,
720+
col_space: Optional[int] = None,
721+
header: Union[bool, Sequence[str]] = True,
722+
index: bool = True,
723+
na_rep: str = "NaN",
724+
formatters: Optional[fmt.formatters_type] = None,
725+
float_format: Optional[fmt.float_format_type] = None,
726+
sparsify: Optional[bool] = None,
727+
index_names: bool = True,
728+
justify: Optional[str] = None,
729+
max_rows: Optional[int] = None,
730+
min_rows: Optional[int] = None,
731+
max_cols: Optional[int] = None,
732+
show_dimensions: bool = False,
733+
decimal: str = ".",
734+
line_width: Optional[int] = None,
735+
max_colwidth: Optional[int] = None,
736+
) -> Optional[str]:
736737
"""
737738
Render a DataFrame to a console-friendly tabular output.
738739
%(shared_params)s
@@ -2162,6 +2163,7 @@ def to_parquet(
21622163
)
21632164

21642165
@Substitution(
2166+
header_type="bool",
21652167
header="Whether to print column labels, default True",
21662168
col_space_type="str or int",
21672169
col_space="The minimum width of each column in CSS length "

pandas/io/formats/format.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Iterable,
2222
List,
2323
Optional,
24+
Sequence,
2425
Tuple,
2526
Type,
2627
Union,
@@ -90,7 +91,7 @@
9091
The subset of columns to write. Writes all columns by default.
9192
col_space : %(col_space_type)s, optional
9293
%(col_space)s.
93-
header : bool, optional
94+
header : %(header_type)s, optional
9495
%(header)s.
9596
index : bool, optional, default True
9697
Whether to print index (row) labels.
@@ -530,9 +531,9 @@ class DataFrameFormatter(TableFormatter):
530531
def __init__(
531532
self,
532533
frame: "DataFrame",
533-
columns: Optional[List[str]] = None,
534+
columns: Optional[Sequence[str]] = None,
534535
col_space: Optional[Union[str, int]] = None,
535-
header: Union[bool, List[str]] = True,
536+
header: Union[bool, Sequence[str]] = True,
536537
index: bool = True,
537538
na_rep: str = "NaN",
538539
formatters: Optional[formatters_type] = None,

0 commit comments

Comments
 (0)