Skip to content

Commit 031fb16

Browse files
authored
CLN: Index._format_with_header (remove kwargs etc.) (#35118)
1 parent 0dffbdf commit 031fb16

File tree

6 files changed

+38
-19
lines changed

6 files changed

+38
-19
lines changed

pandas/core/indexes/base.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
from datetime import datetime
33
import operator
44
from textwrap import dedent
5-
from typing import TYPE_CHECKING, Any, Callable, FrozenSet, Hashable, Optional, Union
5+
from typing import (
6+
TYPE_CHECKING,
7+
Any,
8+
Callable,
9+
FrozenSet,
10+
Hashable,
11+
List,
12+
Optional,
13+
Union,
14+
)
615
import warnings
716

817
import numpy as np
@@ -910,15 +919,12 @@ def format(self, name: bool = False, formatter=None, **kwargs):
910919

911920
return self._format_with_header(header, **kwargs)
912921

913-
def _format_with_header(self, header, na_rep="NaN", **kwargs):
914-
values = self._values
915-
922+
def _format_with_header(self, header, na_rep="NaN") -> List[str_t]:
916923
from pandas.io.formats.format import format_array
917924

918-
if is_categorical_dtype(values.dtype):
919-
values = np.array(values)
925+
values = self._values
920926

921-
elif is_object_dtype(values.dtype):
927+
if is_object_dtype(values.dtype):
922928
values = lib.maybe_convert_objects(values, safe=1)
923929

924930
if is_object_dtype(values.dtype):
@@ -929,10 +935,9 @@ def _format_with_header(self, header, na_rep="NaN", **kwargs):
929935
if mask.any():
930936
result = np.array(result)
931937
result[mask] = na_rep
932-
result = result.tolist()
933-
938+
result = result.tolist() # type: ignore
934939
else:
935-
result = _trim_front(format_array(values, None, justify="left"))
940+
result = trim_front(format_array(values, None, justify="left"))
936941
return header + result
937942

938943
def to_native_types(self, slicer=None, **kwargs):
@@ -5611,7 +5616,7 @@ def ensure_has_len(seq):
56115616
return seq
56125617

56135618

5614-
def _trim_front(strings):
5619+
def trim_front(strings: List[str]) -> List[str]:
56155620
"""
56165621
Trims zeros and decimal points.
56175622
"""

pandas/core/indexes/category.py

+9
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,15 @@ def _format_attrs(self):
347347
attrs.append(("length", len(self)))
348348
return attrs
349349

350+
def _format_with_header(self, header, na_rep="NaN") -> List[str]:
351+
from pandas.io.formats.format import format_array
352+
353+
formatted_values = format_array(
354+
self._values, formatter=None, na_rep=na_rep, justify="left"
355+
)
356+
result = ibase.trim_front(formatted_values)
357+
return header + result
358+
350359
# --------------------------------------------------------------------
351360

352361
@property

pandas/core/indexes/datetimelike.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
338338
# --------------------------------------------------------------------
339339
# Rendering Methods
340340

341-
def _format_with_header(self, header, na_rep="NaT", **kwargs):
342-
return header + list(self._format_native_types(na_rep, **kwargs))
341+
def _format_with_header(self, header, na_rep="NaT", date_format=None) -> List[str]:
342+
return header + list(
343+
self._format_native_types(na_rep=na_rep, date_format=date_format)
344+
)
343345

344346
@property
345347
def _formatter_func(self):

pandas/core/indexes/interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" define the IntervalIndex """
22
from operator import le, lt
33
import textwrap
4-
from typing import Any, Optional, Tuple, Union
4+
from typing import Any, List, Optional, Tuple, Union
55

66
import numpy as np
77

@@ -948,8 +948,8 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
948948
# Rendering Methods
949949
# __repr__ associated methods are based on MultiIndex
950950

951-
def _format_with_header(self, header, **kwargs):
952-
return header + list(self._format_native_types(**kwargs))
951+
def _format_with_header(self, header, na_rep="NaN") -> List[str]:
952+
return header + list(self._format_native_types(na_rep=na_rep))
953953

954954
def _format_native_types(self, na_rep="NaN", quoting=None, **kwargs):
955955
# GH 28210: use base method but with different default na_rep

pandas/core/indexes/range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22
import operator
33
from sys import getsizeof
4-
from typing import Any, Optional
4+
from typing import Any, List, Optional
55
import warnings
66

77
import numpy as np
@@ -197,7 +197,7 @@ def _format_data(self, name=None):
197197
# we are formatting thru the attributes
198198
return None
199199

200-
def _format_with_header(self, header, na_rep="NaN", **kwargs):
200+
def _format_with_header(self, header, na_rep="NaN") -> List[str]:
201201
return header + list(map(pprint_thing, self._range))
202202

203203
# --------------------------------------------------------------------

pandas/io/formats/style.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,10 @@ def _get_level_lengths(index, hidden_elements=None):
15241524
15251525
Result is a dictionary of (level, initial_position): span
15261526
"""
1527-
levels = index.format(sparsify=lib.no_default, adjoin=False, names=False)
1527+
if isinstance(index, pd.MultiIndex):
1528+
levels = index.format(sparsify=lib.no_default, adjoin=False)
1529+
else:
1530+
levels = index.format()
15281531

15291532
if hidden_elements is None:
15301533
hidden_elements = []

0 commit comments

Comments
 (0)