Skip to content

Commit 6c58a21

Browse files
authored
REF: improve privatization in io.formats.format (#55389)
* REF: privatize where possible in io.formats.format * REF: de-privatize VALID_JUSTIFY_PARAMETERS, avoid multiple get_option calls
1 parent d943c26 commit 6c58a21

File tree

5 files changed

+73
-72
lines changed

5 files changed

+73
-72
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ def to_html(
32663266
... </table>'''
32673267
>>> assert html_string == df.to_html()
32683268
"""
3269-
if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS:
3269+
if justify is not None and justify not in fmt.VALID_JUSTIFY_PARAMETERS:
32703270
raise ValueError("Invalid value for justify parameter")
32713271

32723272
formatter = fmt.DataFrameFormatter(

pandas/io/formats/format.py

+41-40
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import re
2121
from shutil import get_terminal_size
2222
from typing import (
23-
IO,
2423
TYPE_CHECKING,
2524
Any,
2625
Callable,
@@ -172,7 +171,7 @@
172171
Character recognized as decimal separator, e.g. ',' in Europe.
173172
"""
174173

175-
_VALID_JUSTIFY_PARAMETERS = (
174+
VALID_JUSTIFY_PARAMETERS = (
176175
"left",
177176
"right",
178177
"center",
@@ -196,10 +195,15 @@
196195

197196

198197
class SeriesFormatter:
198+
"""
199+
Implement the main logic of Series.to_string, which underlies
200+
Series.__repr__.
201+
"""
202+
199203
def __init__(
200204
self,
201205
series: Series,
202-
buf: IO[str] | None = None,
206+
*,
203207
length: bool | str = True,
204208
header: bool = True,
205209
index: bool = True,
@@ -211,7 +215,7 @@ def __init__(
211215
min_rows: int | None = None,
212216
) -> None:
213217
self.series = series
214-
self.buf = buf if buf is not None else StringIO()
218+
self.buf = StringIO()
215219
self.name = name
216220
self.na_rep = na_rep
217221
self.header = header
@@ -355,7 +359,7 @@ def to_string(self) -> str:
355359
return str("".join(result))
356360

357361

358-
class TextAdjustment:
362+
class _TextAdjustment:
359363
def __init__(self) -> None:
360364
self.encoding = get_option("display.encoding")
361365

@@ -371,7 +375,7 @@ def adjoin(self, space: int, *lists, **kwargs) -> str:
371375
)
372376

373377

374-
class EastAsianTextAdjustment(TextAdjustment):
378+
class _EastAsianTextAdjustment(_TextAdjustment):
375379
def __init__(self) -> None:
376380
super().__init__()
377381
if get_option("display.unicode.ambiguous_as_wide"):
@@ -410,12 +414,12 @@ def _get_pad(t):
410414
return [x.rjust(_get_pad(x)) for x in texts]
411415

412416

413-
def get_adjustment() -> TextAdjustment:
417+
def get_adjustment() -> _TextAdjustment:
414418
use_east_asian_width = get_option("display.unicode.east_asian_width")
415419
if use_east_asian_width:
416-
return EastAsianTextAdjustment()
420+
return _EastAsianTextAdjustment()
417421
else:
418-
return TextAdjustment()
422+
return _TextAdjustment()
419423

420424

421425
def get_dataframe_repr_params() -> dict[str, Any]:
@@ -469,16 +473,9 @@ def get_series_repr_params() -> dict[str, Any]:
469473
True
470474
"""
471475
width, height = get_terminal_size()
472-
max_rows = (
473-
height
474-
if get_option("display.max_rows") == 0
475-
else get_option("display.max_rows")
476-
)
477-
min_rows = (
478-
height
479-
if get_option("display.max_rows") == 0
480-
else get_option("display.min_rows")
481-
)
476+
max_rows_opt = get_option("display.max_rows")
477+
max_rows = height if max_rows_opt == 0 else max_rows_opt
478+
min_rows = height if max_rows_opt == 0 else get_option("display.min_rows")
482479

483480
return {
484481
"name": True,
@@ -490,7 +487,11 @@ def get_series_repr_params() -> dict[str, Any]:
490487

491488

492489
class DataFrameFormatter:
493-
"""Class for processing dataframe formatting options and data."""
490+
"""
491+
Class for processing dataframe formatting options and data.
492+
493+
Used by DataFrame.to_string, which backs DataFrame.__repr__.
494+
"""
494495

495496
__doc__ = __doc__ if __doc__ else ""
496497
__doc__ += common_docstring + return_docstring
@@ -1102,16 +1103,16 @@ def save_to_buffer(
11021103
"""
11031104
Perform serialization. Write to buf or return as string if buf is None.
11041105
"""
1105-
with get_buffer(buf, encoding=encoding) as f:
1106-
f.write(string)
1106+
with _get_buffer(buf, encoding=encoding) as fd:
1107+
fd.write(string)
11071108
if buf is None:
11081109
# error: "WriteBuffer[str]" has no attribute "getvalue"
1109-
return f.getvalue() # type: ignore[attr-defined]
1110+
return fd.getvalue() # type: ignore[attr-defined]
11101111
return None
11111112

11121113

11131114
@contextmanager
1114-
def get_buffer(
1115+
def _get_buffer(
11151116
buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None
11161117
) -> Generator[WriteBuffer[str], None, None] | Generator[StringIO, None, None]:
11171118
"""
@@ -1188,24 +1189,24 @@ def format_array(
11881189
-------
11891190
List[str]
11901191
"""
1191-
fmt_klass: type[GenericArrayFormatter]
1192+
fmt_klass: type[_GenericArrayFormatter]
11921193
if lib.is_np_dtype(values.dtype, "M"):
1193-
fmt_klass = Datetime64Formatter
1194+
fmt_klass = _Datetime64Formatter
11941195
values = cast(DatetimeArray, values)
11951196
elif isinstance(values.dtype, DatetimeTZDtype):
1196-
fmt_klass = Datetime64TZFormatter
1197+
fmt_klass = _Datetime64TZFormatter
11971198
values = cast(DatetimeArray, values)
11981199
elif lib.is_np_dtype(values.dtype, "m"):
1199-
fmt_klass = Timedelta64Formatter
1200+
fmt_klass = _Timedelta64Formatter
12001201
values = cast(TimedeltaArray, values)
12011202
elif isinstance(values.dtype, ExtensionDtype):
1202-
fmt_klass = ExtensionArrayFormatter
1203+
fmt_klass = _ExtensionArrayFormatter
12031204
elif lib.is_np_dtype(values.dtype, "fc"):
12041205
fmt_klass = FloatArrayFormatter
12051206
elif lib.is_np_dtype(values.dtype, "iu"):
1206-
fmt_klass = IntArrayFormatter
1207+
fmt_klass = _IntArrayFormatter
12071208
else:
1208-
fmt_klass = GenericArrayFormatter
1209+
fmt_klass = _GenericArrayFormatter
12091210

12101211
if space is None:
12111212
space = 12
@@ -1233,7 +1234,7 @@ def format_array(
12331234
return fmt_obj.get_result()
12341235

12351236

1236-
class GenericArrayFormatter:
1237+
class _GenericArrayFormatter:
12371238
def __init__(
12381239
self,
12391240
values: ArrayLike,
@@ -1315,7 +1316,7 @@ def _format(x):
13151316
vals = extract_array(self.values, extract_numpy=True)
13161317
if not isinstance(vals, np.ndarray):
13171318
raise TypeError(
1318-
"ExtensionArray formatting should use ExtensionArrayFormatter"
1319+
"ExtensionArray formatting should use _ExtensionArrayFormatter"
13191320
)
13201321
inferred = lib.map_infer(vals, is_float)
13211322
is_float_type = (
@@ -1345,7 +1346,7 @@ def _format(x):
13451346
return fmt_values
13461347

13471348

1348-
class FloatArrayFormatter(GenericArrayFormatter):
1349+
class FloatArrayFormatter(_GenericArrayFormatter):
13491350
def __init__(self, *args, **kwargs) -> None:
13501351
super().__init__(*args, **kwargs)
13511352

@@ -1546,7 +1547,7 @@ def _format_strings(self) -> list[str]:
15461547
return list(self.get_result_as_array())
15471548

15481549

1549-
class IntArrayFormatter(GenericArrayFormatter):
1550+
class _IntArrayFormatter(_GenericArrayFormatter):
15501551
def _format_strings(self) -> list[str]:
15511552
if self.leading_space is False:
15521553
formatter_str = lambda x: f"{x:d}".format(x=x)
@@ -1557,7 +1558,7 @@ def _format_strings(self) -> list[str]:
15571558
return fmt_values
15581559

15591560

1560-
class Datetime64Formatter(GenericArrayFormatter):
1561+
class _Datetime64Formatter(_GenericArrayFormatter):
15611562
values: DatetimeArray
15621563

15631564
def __init__(
@@ -1586,7 +1587,7 @@ def _format_strings(self) -> list[str]:
15861587
return fmt_values.tolist()
15871588

15881589

1589-
class ExtensionArrayFormatter(GenericArrayFormatter):
1590+
class _ExtensionArrayFormatter(_GenericArrayFormatter):
15901591
values: ExtensionArray
15911592

15921593
def _format_strings(self) -> list[str]:
@@ -1727,7 +1728,7 @@ def get_format_datetime64(
17271728
return lambda x: _format_datetime64(x, nat_rep=nat_rep)
17281729

17291730

1730-
class Datetime64TZFormatter(Datetime64Formatter):
1731+
class _Datetime64TZFormatter(_Datetime64Formatter):
17311732
values: DatetimeArray
17321733

17331734
def _format_strings(self) -> list[str]:
@@ -1742,7 +1743,7 @@ def _format_strings(self) -> list[str]:
17421743
return fmt_values
17431744

17441745

1745-
class Timedelta64Formatter(GenericArrayFormatter):
1746+
class _Timedelta64Formatter(_GenericArrayFormatter):
17461747
values: TimedeltaArray
17471748

17481749
def __init__(
@@ -1809,7 +1810,7 @@ def _make_fixed_width(
18091810
strings: list[str],
18101811
justify: str = "right",
18111812
minimum: int | None = None,
1812-
adj: TextAdjustment | None = None,
1813+
adj: _TextAdjustment | None = None,
18131814
) -> list[str]:
18141815
if len(strings) == 0 or justify == "all":
18151816
return strings

0 commit comments

Comments
 (0)