Skip to content

Commit

Permalink
CLN: Index._format_with_header (remove kwargs etc.) (#35118)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored Jul 7, 2020
1 parent 0dffbdf commit 031fb16
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
27 changes: 16 additions & 11 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import TYPE_CHECKING, Any, Callable, FrozenSet, Hashable, Optional, Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
FrozenSet,
Hashable,
List,
Optional,
Union,
)
import warnings

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

return self._format_with_header(header, **kwargs)

def _format_with_header(self, header, na_rep="NaN", **kwargs):
values = self._values

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

if is_categorical_dtype(values.dtype):
values = np.array(values)
values = self._values

elif is_object_dtype(values.dtype):
if is_object_dtype(values.dtype):
values = lib.maybe_convert_objects(values, safe=1)

if is_object_dtype(values.dtype):
Expand All @@ -929,10 +935,9 @@ def _format_with_header(self, header, na_rep="NaN", **kwargs):
if mask.any():
result = np.array(result)
result[mask] = na_rep
result = result.tolist()

result = result.tolist() # type: ignore
else:
result = _trim_front(format_array(values, None, justify="left"))
result = trim_front(format_array(values, None, justify="left"))
return header + result

def to_native_types(self, slicer=None, **kwargs):
Expand Down Expand Up @@ -5611,7 +5616,7 @@ def ensure_has_len(seq):
return seq


def _trim_front(strings):
def trim_front(strings: List[str]) -> List[str]:
"""
Trims zeros and decimal points.
"""
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ def _format_attrs(self):
attrs.append(("length", len(self)))
return attrs

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

formatted_values = format_array(
self._values, formatter=None, na_rep=na_rep, justify="left"
)
result = ibase.trim_front(formatted_values)
return header + result

# --------------------------------------------------------------------

@property
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
# --------------------------------------------------------------------
# Rendering Methods

def _format_with_header(self, header, na_rep="NaT", **kwargs):
return header + list(self._format_native_types(na_rep, **kwargs))
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)
)

@property
def _formatter_func(self):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" define the IntervalIndex """
from operator import le, lt
import textwrap
from typing import Any, Optional, Tuple, Union
from typing import Any, List, Optional, Tuple, Union

import numpy as np

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

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

def _format_native_types(self, na_rep="NaN", quoting=None, **kwargs):
# GH 28210: use base method but with different default na_rep
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import operator
from sys import getsizeof
from typing import Any, Optional
from typing import Any, List, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -197,7 +197,7 @@ def _format_data(self, name=None):
# we are formatting thru the attributes
return None

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

# --------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,10 @@ def _get_level_lengths(index, hidden_elements=None):
Result is a dictionary of (level, initial_position): span
"""
levels = index.format(sparsify=lib.no_default, adjoin=False, names=False)
if isinstance(index, pd.MultiIndex):
levels = index.format(sparsify=lib.no_default, adjoin=False)
else:
levels = index.format()

if hidden_elements is None:
hidden_elements = []
Expand Down

0 comments on commit 031fb16

Please sign in to comment.