Skip to content

Annotate DataFrame Part 2 (Rendering Methods) #28453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import itertools
import sys
from textwrap import dedent
from typing import FrozenSet, List, Optional, Set, Tuple, Type, Union
from typing import FrozenSet, List, Optional, Sequence, Set, Tuple, Type, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -80,7 +80,7 @@
)
from pandas.core.dtypes.missing import isna, notna

from pandas._typing import Axes, Dtype
from pandas._typing import Axes, Dtype, FilePathOrBuffer
from pandas.core import algorithms, common as com, nanops, ops
from pandas.core.accessor import CachedAccessor
from pandas.core.arrays import Categorical, ExtensionArray
Expand Down Expand Up @@ -559,14 +559,14 @@ def _is_homogeneous_type(self) -> bool:
# ----------------------------------------------------------------------
# Rendering Methods

def _repr_fits_vertical_(self):
def _repr_fits_vertical_(self) -> bool:
"""
Check length against max_rows.
"""
max_rows = get_option("display.max_rows")
return len(self) <= max_rows

def _repr_fits_horizontal_(self, ignore_width=False):
def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
"""
Check if full repr fits in horizontal boundaries imposed by the display
options width and max_columns.
Expand Down Expand Up @@ -620,7 +620,7 @@ def _repr_fits_horizontal_(self, ignore_width=False):

return repr_width < width

def _info_repr(self):
def _info_repr(self) -> bool:
"""
True if the repr should show the info view.
"""
Expand All @@ -629,7 +629,7 @@ def _info_repr(self):
self._repr_fits_horizontal_() and self._repr_fits_vertical_()
)

def __repr__(self):
def __repr__(self) -> str:
"""
Return a string representation for a particular DataFrame.
"""
Expand Down Expand Up @@ -659,7 +659,7 @@ def __repr__(self):

return buf.getvalue()

def _repr_html_(self):
def _repr_html_(self) -> Optional[str]:
"""
Return a html representation for a particular DataFrame.

Expand Down Expand Up @@ -706,6 +706,7 @@ def _repr_html_(self):
return None

@Substitution(
header_type="bool or sequence",
header="Write out the column names. If a list of strings "
"is given, it is assumed to be aliases for the "
"column names",
Expand All @@ -715,25 +716,25 @@ def _repr_html_(self):
@Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring)
def to_string(
self,
buf=None,
columns=None,
col_space=None,
header=True,
index=True,
na_rep="NaN",
formatters=None,
float_format=None,
sparsify=None,
index_names=True,
justify=None,
max_rows=None,
min_rows=None,
max_cols=None,
show_dimensions=False,
decimal=".",
line_width=None,
max_colwidth=None,
):
buf: Optional[FilePathOrBuffer[str]] = None,
columns: Optional[Sequence[str]] = None,
col_space: Optional[int] = None,
header: Union[bool, Sequence[str]] = True,
index: bool = True,
na_rep: str = "NaN",
formatters: Optional[fmt.formatters_type] = None,
float_format: Optional[fmt.float_format_type] = None,
sparsify: Optional[bool] = None,
index_names: bool = True,
justify: Optional[str] = None,
max_rows: Optional[int] = None,
min_rows: Optional[int] = None,
max_cols: Optional[int] = None,
show_dimensions: bool = False,
decimal: str = ".",
line_width: Optional[int] = None,
max_colwidth: Optional[int] = None,
) -> Optional[str]:
"""
Render a DataFrame to a console-friendly tabular output.
%(shared_params)s
Expand Down Expand Up @@ -2234,6 +2235,7 @@ def to_parquet(
)

@Substitution(
header_type="bool",
header="Whether to print column labels, default True",
col_space_type="str or int",
col_space="The minimum width of each column in CSS length "
Expand Down
7 changes: 4 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Iterable,
List,
Optional,
Sequence,
Tuple,
Type,
Union,
Expand Down Expand Up @@ -90,7 +91,7 @@
The subset of columns to write. Writes all columns by default.
col_space : %(col_space_type)s, optional
%(col_space)s.
header : bool, optional
header : %(header_type)s, optional
%(header)s.
index : bool, optional, default True
Whether to print index (row) labels.
Expand Down Expand Up @@ -530,9 +531,9 @@ class DataFrameFormatter(TableFormatter):
def __init__(
self,
frame: "DataFrame",
columns: Optional[List[str]] = None,
columns: Optional[Sequence[str]] = None,
col_space: Optional[Union[str, int]] = None,
header: Union[bool, List[str]] = True,
header: Union[bool, Sequence[str]] = True,
index: bool = True,
na_rep: str = "NaN",
formatters: Optional[formatters_type] = None,
Expand Down