Skip to content

Commit

Permalink
CLN: remove kwargs in Index.format (#35122)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored Jul 14, 2020
1 parent b6222ec commit b0468aa
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
9 changes: 7 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,12 @@ def _mpl_repr(self):
# how to represent ourselves to matplotlib
return self.values

def format(self, name: bool = False, formatter=None, **kwargs):
def format(
self,
name: bool = False,
formatter: Optional[Callable] = None,
na_rep: str_t = "NaN",
) -> List[str_t]:
"""
Render a string representation of the Index.
"""
Expand All @@ -917,7 +922,7 @@ def format(self, name: bool = False, formatter=None, **kwargs):
if formatter is not None:
return header + list(self.map(formatter))

return self._format_with_header(header, **kwargs)
return self._format_with_header(header, na_rep=na_rep)

def _format_with_header(self, header, na_rep="NaN") -> List[str_t]:
from pandas.io.formats.format import format_array
Expand Down
22 changes: 21 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pandas._libs import NaT, Timedelta, iNaT, join as libjoin, lib
from pandas._libs.tslibs import BaseOffset, Resolution, Tick, timezones
from pandas._libs.tslibs.parsing import DateParseError
from pandas._typing import Label
from pandas._typing import Callable, Label
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, cache_readonly, doc
Expand Down Expand Up @@ -338,6 +338,26 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
# --------------------------------------------------------------------
# Rendering Methods

def format(
self,
name: bool = False,
formatter: Optional[Callable] = None,
na_rep: str = "NaT",
date_format: Optional[str] = None,
) -> List[str]:
"""
Render a string representation of the Index.
"""
header = []
if name:
fmt_name = ibase.pprint_thing(self.name, escape_chars=("\t", "\r", "\n"))
header.append(fmt_name)

if formatter is not None:
return header + list(self.map(formatter))

return self._format_with_header(header, na_rep=na_rep, date_format=date_format)

def _format_with_header(self, header, na_rep="NaT", date_format=None) -> List[str]:
return header + list(
self._format_native_types(na_rep=na_rep, date_format=date_format)
Expand Down
30 changes: 17 additions & 13 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import (
TYPE_CHECKING,
Any,
Callable,
Hashable,
Iterable,
List,
Expand Down Expand Up @@ -1231,13 +1232,17 @@ def _format_native_types(self, na_rep="nan", **kwargs):

def format(
self,
space=2,
name: Optional[bool] = None,
formatter: Optional[Callable] = None,
na_rep: Optional[str] = None,
names: bool = False,
space: int = 2,
sparsify=None,
adjoin=True,
names=False,
na_rep=None,
formatter=None,
):
adjoin: bool = True,
) -> List:
if name is not None:
names = name

if len(self) == 0:
return []

Expand Down Expand Up @@ -1265,13 +1270,13 @@ def format(
stringified_levels.append(formatted)

result_levels = []
for lev, name in zip(stringified_levels, self.names):
for lev, lev_name in zip(stringified_levels, self.names):
level = []

if names:
level.append(
pprint_thing(name, escape_chars=("\t", "\r", "\n"))
if name is not None
pprint_thing(lev_name, escape_chars=("\t", "\r", "\n"))
if lev_name is not None
else ""
)

Expand All @@ -1283,10 +1288,9 @@ def format(

if sparsify:
sentinel = ""
# GH3547
# use value of sparsify as sentinel, unless it's an obvious
# "Truthy" value
if sparsify not in [True, 1]:
# GH3547 use value of sparsify as sentinel if it's "Falsey"
assert isinstance(sparsify, bool) or sparsify is lib.no_default
if sparsify in [False, lib.no_default]:
sentinel = sparsify
# little bit of a kludge job for #1217
result_levels = _sparsify(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _format_data(self, name=None):
return None

def _format_with_header(self, header, na_rep="NaN") -> List[str]:
return header + list(map(pprint_thing, self._range))
return header + [pprint_thing(x) for x in self._range]

# --------------------------------------------------------------------
_deprecation_message = (
Expand Down
3 changes: 1 addition & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,8 @@ def _get_footer(self) -> str:

def _get_formatted_index(self) -> Tuple[List[str], bool]:
index = self.tr_series.index
is_multi = isinstance(index, MultiIndex)

if is_multi:
if isinstance(index, MultiIndex):
have_header = any(name for name in index.names)
fmt_index = index.format(names=True)
else:
Expand Down
1 change: 1 addition & 0 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def _write_hierarchical_rows(
frame = self.fmt.tr_frame
nrows = len(frame)

assert isinstance(frame.index, MultiIndex)
idx_values = frame.index.format(sparsify=False, adjoin=False, names=False)
idx_values = list(zip(*idx_values))

Expand Down

0 comments on commit b0468aa

Please sign in to comment.