Skip to content

Commit 52d6a16

Browse files
committed
REF: replace formatters setter with initialization
1 parent 186c98f commit 52d6a16

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

pandas/io/formats/format.py

+8-28
Original file line numberDiff line numberDiff line change
@@ -453,20 +453,13 @@ def get_adjustment() -> TextAdjustment:
453453
class TableFormatter:
454454

455455
show_dimensions: Union[bool, str]
456+
formatters: FormattersType
456457

457458
@property
458459
def is_truncated(self) -> bool:
459460
self._is_truncated: bool
460461
return self._is_truncated
461462

462-
@property
463-
def formatters(self) -> FormattersType:
464-
return self._formatters
465-
466-
@formatters.setter
467-
def formatters(self, formatters: FormattersType) -> None:
468-
self._formatters: FormattersType = formatters
469-
470463
@property
471464
def columns(self) -> Index:
472465
return self._columns
@@ -585,15 +578,7 @@ def __init__(
585578
self.show_index_names = index_names
586579
self.sparsify = self._initialize_sparsify(sparsify)
587580
self.float_format = float_format
588-
589-
# Ignoring error
590-
# expression has type "Union[List[Callable[..., Any]],
591-
# Tuple[Callable[..., Any], ...],
592-
# Mapping[Union[str, int], Callable[..., Any]], None]",
593-
# variable has type "Union[List[Callable[..., Any]],
594-
# Tuple[Callable[..., Any], ...], Mapping[Union[str, int],
595-
# Callable[..., Any]]]")
596-
self.formatters = formatters # type: ignore[assignment]
581+
self.formatters = self._initialize_formatters(formatters)
597582
self.na_rep = na_rep
598583
self.decimal = decimal
599584

@@ -628,23 +613,18 @@ def _initialize_sparsify(self, sparsify: Optional[bool]) -> bool:
628613
return get_option("display.multi_sparse")
629614
return sparsify
630615

631-
@property
632-
def formatters(self) -> FormattersType:
633-
return self._formatters
634-
635-
@formatters.setter
636-
def formatters(self, formatters: Optional[FormattersType]) -> None:
637-
self._formatters: FormattersType
616+
def _initialize_formatters(
617+
self, formatters: Optional[FormattersType]
618+
) -> FormattersType:
638619
if formatters is None:
639-
self._formatters = {}
620+
return {}
640621
elif len(self.frame.columns) == len(formatters) or isinstance(formatters, dict):
641-
self._formatters = formatters
622+
return formatters
642623
else:
643624
raise ValueError(
644625
f"Formatters length({len(formatters)}) should match "
645626
f"DataFrame number of columns({len(self.frame.columns)})"
646627
)
647-
assert self._formatters is not None
648628

649629
@property
650630
def justify(self) -> str:
@@ -813,7 +793,7 @@ def _truncate_horizontally(self) -> None:
813793
# truncate formatter
814794
if isinstance(self.formatters, (list, tuple)):
815795
slicer = itemgetter(*cols_to_keep)
816-
self._formatters = slicer(self.formatters)
796+
self.formatters = slicer(self.formatters)
817797
else:
818798
col_num = cast(int, self.max_cols)
819799
self.tr_frame = self.tr_frame.iloc[:, :col_num]

0 commit comments

Comments
 (0)