Skip to content

Commit 426aa26

Browse files
topper-123fangchenli
authored andcommitted
CLN: remove kwargs in Index.format (pandas-dev#35122)
1 parent 935be95 commit 426aa26

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

pandas/core/indexes/base.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,12 @@ def _mpl_repr(self):
902902
# how to represent ourselves to matplotlib
903903
return self.values
904904

905-
def format(self, name: bool = False, formatter=None, **kwargs):
905+
def format(
906+
self,
907+
name: bool = False,
908+
formatter: Optional[Callable] = None,
909+
na_rep: str_t = "NaN",
910+
) -> List[str_t]:
906911
"""
907912
Render a string representation of the Index.
908913
"""
@@ -917,7 +922,7 @@ def format(self, name: bool = False, formatter=None, **kwargs):
917922
if formatter is not None:
918923
return header + list(self.map(formatter))
919924

920-
return self._format_with_header(header, **kwargs)
925+
return self._format_with_header(header, na_rep=na_rep)
921926

922927
def _format_with_header(self, header, na_rep="NaN") -> List[str_t]:
923928
from pandas.io.formats.format import format_array

pandas/core/indexes/datetimelike.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pandas._libs import NaT, Timedelta, iNaT, join as libjoin, lib
1010
from pandas._libs.tslibs import BaseOffset, Resolution, Tick, timezones
1111
from pandas._libs.tslibs.parsing import DateParseError
12-
from pandas._typing import Label
12+
from pandas._typing import Callable, Label
1313
from pandas.compat.numpy import function as nv
1414
from pandas.errors import AbstractMethodError
1515
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -338,6 +338,26 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
338338
# --------------------------------------------------------------------
339339
# Rendering Methods
340340

341+
def format(
342+
self,
343+
name: bool = False,
344+
formatter: Optional[Callable] = None,
345+
na_rep: str = "NaT",
346+
date_format: Optional[str] = None,
347+
) -> List[str]:
348+
"""
349+
Render a string representation of the Index.
350+
"""
351+
header = []
352+
if name:
353+
fmt_name = ibase.pprint_thing(self.name, escape_chars=("\t", "\r", "\n"))
354+
header.append(fmt_name)
355+
356+
if formatter is not None:
357+
return header + list(self.map(formatter))
358+
359+
return self._format_with_header(header, na_rep=na_rep, date_format=date_format)
360+
341361
def _format_with_header(self, header, na_rep="NaT", date_format=None) -> List[str]:
342362
return header + list(
343363
self._format_native_types(na_rep=na_rep, date_format=date_format)

pandas/core/indexes/multi.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import (
33
TYPE_CHECKING,
44
Any,
5+
Callable,
56
Hashable,
67
Iterable,
78
List,
@@ -1231,13 +1232,17 @@ def _format_native_types(self, na_rep="nan", **kwargs):
12311232

12321233
def format(
12331234
self,
1234-
space=2,
1235+
name: Optional[bool] = None,
1236+
formatter: Optional[Callable] = None,
1237+
na_rep: Optional[str] = None,
1238+
names: bool = False,
1239+
space: int = 2,
12351240
sparsify=None,
1236-
adjoin=True,
1237-
names=False,
1238-
na_rep=None,
1239-
formatter=None,
1240-
):
1241+
adjoin: bool = True,
1242+
) -> List:
1243+
if name is not None:
1244+
names = name
1245+
12411246
if len(self) == 0:
12421247
return []
12431248

@@ -1265,13 +1270,13 @@ def format(
12651270
stringified_levels.append(formatted)
12661271

12671272
result_levels = []
1268-
for lev, name in zip(stringified_levels, self.names):
1273+
for lev, lev_name in zip(stringified_levels, self.names):
12691274
level = []
12701275

12711276
if names:
12721277
level.append(
1273-
pprint_thing(name, escape_chars=("\t", "\r", "\n"))
1274-
if name is not None
1278+
pprint_thing(lev_name, escape_chars=("\t", "\r", "\n"))
1279+
if lev_name is not None
12751280
else ""
12761281
)
12771282

@@ -1283,10 +1288,9 @@ def format(
12831288

12841289
if sparsify:
12851290
sentinel = ""
1286-
# GH3547
1287-
# use value of sparsify as sentinel, unless it's an obvious
1288-
# "Truthy" value
1289-
if sparsify not in [True, 1]:
1291+
# GH3547 use value of sparsify as sentinel if it's "Falsey"
1292+
assert isinstance(sparsify, bool) or sparsify is lib.no_default
1293+
if sparsify in [False, lib.no_default]:
12901294
sentinel = sparsify
12911295
# little bit of a kludge job for #1217
12921296
result_levels = _sparsify(

pandas/core/indexes/range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _format_data(self, name=None):
198198
return None
199199

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

203203
# --------------------------------------------------------------------
204204
_deprecation_message = (

pandas/io/formats/format.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,8 @@ def _get_footer(self) -> str:
330330

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

335-
if is_multi:
334+
if isinstance(index, MultiIndex):
336335
have_header = any(name for name in index.names)
337336
fmt_index = index.format(names=True)
338337
else:

pandas/io/formats/html.py

+1
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def _write_hierarchical_rows(
442442
frame = self.fmt.tr_frame
443443
nrows = len(frame)
444444

445+
assert isinstance(frame.index, MultiIndex)
445446
idx_values = frame.index.format(sparsify=False, adjoin=False, names=False)
446447
idx_values = list(zip(*idx_values))
447448

0 commit comments

Comments
 (0)