20
20
import re
21
21
from shutil import get_terminal_size
22
22
from typing import (
23
- IO ,
24
23
TYPE_CHECKING ,
25
24
Any ,
26
25
Callable ,
172
171
Character recognized as decimal separator, e.g. ',' in Europe.
173
172
"""
174
173
175
- _VALID_JUSTIFY_PARAMETERS = (
174
+ VALID_JUSTIFY_PARAMETERS = (
176
175
"left" ,
177
176
"right" ,
178
177
"center" ,
196
195
197
196
198
197
class SeriesFormatter :
198
+ """
199
+ Implement the main logic of Series.to_string, which underlies
200
+ Series.__repr__.
201
+ """
202
+
199
203
def __init__ (
200
204
self ,
201
205
series : Series ,
202
- buf : IO [ str ] | None = None ,
206
+ * ,
203
207
length : bool | str = True ,
204
208
header : bool = True ,
205
209
index : bool = True ,
@@ -211,7 +215,7 @@ def __init__(
211
215
min_rows : int | None = None ,
212
216
) -> None :
213
217
self .series = series
214
- self .buf = buf if buf is not None else StringIO ()
218
+ self .buf = StringIO ()
215
219
self .name = name
216
220
self .na_rep = na_rep
217
221
self .header = header
@@ -355,7 +359,7 @@ def to_string(self) -> str:
355
359
return str ("" .join (result ))
356
360
357
361
358
- class TextAdjustment :
362
+ class _TextAdjustment :
359
363
def __init__ (self ) -> None :
360
364
self .encoding = get_option ("display.encoding" )
361
365
@@ -371,7 +375,7 @@ def adjoin(self, space: int, *lists, **kwargs) -> str:
371
375
)
372
376
373
377
374
- class EastAsianTextAdjustment ( TextAdjustment ):
378
+ class _EastAsianTextAdjustment ( _TextAdjustment ):
375
379
def __init__ (self ) -> None :
376
380
super ().__init__ ()
377
381
if get_option ("display.unicode.ambiguous_as_wide" ):
@@ -410,12 +414,12 @@ def _get_pad(t):
410
414
return [x .rjust (_get_pad (x )) for x in texts ]
411
415
412
416
413
- def get_adjustment () -> TextAdjustment :
417
+ def get_adjustment () -> _TextAdjustment :
414
418
use_east_asian_width = get_option ("display.unicode.east_asian_width" )
415
419
if use_east_asian_width :
416
- return EastAsianTextAdjustment ()
420
+ return _EastAsianTextAdjustment ()
417
421
else :
418
- return TextAdjustment ()
422
+ return _TextAdjustment ()
419
423
420
424
421
425
def get_dataframe_repr_params () -> dict [str , Any ]:
@@ -469,16 +473,9 @@ def get_series_repr_params() -> dict[str, Any]:
469
473
True
470
474
"""
471
475
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" )
482
479
483
480
return {
484
481
"name" : True ,
@@ -490,7 +487,11 @@ def get_series_repr_params() -> dict[str, Any]:
490
487
491
488
492
489
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
+ """
494
495
495
496
__doc__ = __doc__ if __doc__ else ""
496
497
__doc__ += common_docstring + return_docstring
@@ -1102,16 +1103,16 @@ def save_to_buffer(
1102
1103
"""
1103
1104
Perform serialization. Write to buf or return as string if buf is None.
1104
1105
"""
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 )
1107
1108
if buf is None :
1108
1109
# error: "WriteBuffer[str]" has no attribute "getvalue"
1109
- return f .getvalue () # type: ignore[attr-defined]
1110
+ return fd .getvalue () # type: ignore[attr-defined]
1110
1111
return None
1111
1112
1112
1113
1113
1114
@contextmanager
1114
- def get_buffer (
1115
+ def _get_buffer (
1115
1116
buf : FilePath | WriteBuffer [str ] | None , encoding : str | None = None
1116
1117
) -> Generator [WriteBuffer [str ], None , None ] | Generator [StringIO , None , None ]:
1117
1118
"""
@@ -1188,24 +1189,24 @@ def format_array(
1188
1189
-------
1189
1190
List[str]
1190
1191
"""
1191
- fmt_klass : type [GenericArrayFormatter ]
1192
+ fmt_klass : type [_GenericArrayFormatter ]
1192
1193
if lib .is_np_dtype (values .dtype , "M" ):
1193
- fmt_klass = Datetime64Formatter
1194
+ fmt_klass = _Datetime64Formatter
1194
1195
values = cast (DatetimeArray , values )
1195
1196
elif isinstance (values .dtype , DatetimeTZDtype ):
1196
- fmt_klass = Datetime64TZFormatter
1197
+ fmt_klass = _Datetime64TZFormatter
1197
1198
values = cast (DatetimeArray , values )
1198
1199
elif lib .is_np_dtype (values .dtype , "m" ):
1199
- fmt_klass = Timedelta64Formatter
1200
+ fmt_klass = _Timedelta64Formatter
1200
1201
values = cast (TimedeltaArray , values )
1201
1202
elif isinstance (values .dtype , ExtensionDtype ):
1202
- fmt_klass = ExtensionArrayFormatter
1203
+ fmt_klass = _ExtensionArrayFormatter
1203
1204
elif lib .is_np_dtype (values .dtype , "fc" ):
1204
1205
fmt_klass = FloatArrayFormatter
1205
1206
elif lib .is_np_dtype (values .dtype , "iu" ):
1206
- fmt_klass = IntArrayFormatter
1207
+ fmt_klass = _IntArrayFormatter
1207
1208
else :
1208
- fmt_klass = GenericArrayFormatter
1209
+ fmt_klass = _GenericArrayFormatter
1209
1210
1210
1211
if space is None :
1211
1212
space = 12
@@ -1233,7 +1234,7 @@ def format_array(
1233
1234
return fmt_obj .get_result ()
1234
1235
1235
1236
1236
- class GenericArrayFormatter :
1237
+ class _GenericArrayFormatter :
1237
1238
def __init__ (
1238
1239
self ,
1239
1240
values : ArrayLike ,
@@ -1315,7 +1316,7 @@ def _format(x):
1315
1316
vals = extract_array (self .values , extract_numpy = True )
1316
1317
if not isinstance (vals , np .ndarray ):
1317
1318
raise TypeError (
1318
- "ExtensionArray formatting should use ExtensionArrayFormatter "
1319
+ "ExtensionArray formatting should use _ExtensionArrayFormatter "
1319
1320
)
1320
1321
inferred = lib .map_infer (vals , is_float )
1321
1322
is_float_type = (
@@ -1345,7 +1346,7 @@ def _format(x):
1345
1346
return fmt_values
1346
1347
1347
1348
1348
- class FloatArrayFormatter (GenericArrayFormatter ):
1349
+ class FloatArrayFormatter (_GenericArrayFormatter ):
1349
1350
def __init__ (self , * args , ** kwargs ) -> None :
1350
1351
super ().__init__ (* args , ** kwargs )
1351
1352
@@ -1546,7 +1547,7 @@ def _format_strings(self) -> list[str]:
1546
1547
return list (self .get_result_as_array ())
1547
1548
1548
1549
1549
- class IntArrayFormatter ( GenericArrayFormatter ):
1550
+ class _IntArrayFormatter ( _GenericArrayFormatter ):
1550
1551
def _format_strings (self ) -> list [str ]:
1551
1552
if self .leading_space is False :
1552
1553
formatter_str = lambda x : f"{ x :d} " .format (x = x )
@@ -1557,7 +1558,7 @@ def _format_strings(self) -> list[str]:
1557
1558
return fmt_values
1558
1559
1559
1560
1560
- class Datetime64Formatter ( GenericArrayFormatter ):
1561
+ class _Datetime64Formatter ( _GenericArrayFormatter ):
1561
1562
values : DatetimeArray
1562
1563
1563
1564
def __init__ (
@@ -1586,7 +1587,7 @@ def _format_strings(self) -> list[str]:
1586
1587
return fmt_values .tolist ()
1587
1588
1588
1589
1589
- class ExtensionArrayFormatter ( GenericArrayFormatter ):
1590
+ class _ExtensionArrayFormatter ( _GenericArrayFormatter ):
1590
1591
values : ExtensionArray
1591
1592
1592
1593
def _format_strings (self ) -> list [str ]:
@@ -1727,7 +1728,7 @@ def get_format_datetime64(
1727
1728
return lambda x : _format_datetime64 (x , nat_rep = nat_rep )
1728
1729
1729
1730
1730
- class Datetime64TZFormatter ( Datetime64Formatter ):
1731
+ class _Datetime64TZFormatter ( _Datetime64Formatter ):
1731
1732
values : DatetimeArray
1732
1733
1733
1734
def _format_strings (self ) -> list [str ]:
@@ -1742,7 +1743,7 @@ def _format_strings(self) -> list[str]:
1742
1743
return fmt_values
1743
1744
1744
1745
1745
- class Timedelta64Formatter ( GenericArrayFormatter ):
1746
+ class _Timedelta64Formatter ( _GenericArrayFormatter ):
1746
1747
values : TimedeltaArray
1747
1748
1748
1749
def __init__ (
@@ -1809,7 +1810,7 @@ def _make_fixed_width(
1809
1810
strings : list [str ],
1810
1811
justify : str = "right" ,
1811
1812
minimum : int | None = None ,
1812
- adj : TextAdjustment | None = None ,
1813
+ adj : _TextAdjustment | None = None ,
1813
1814
) -> list [str ]:
1814
1815
if len (strings ) == 0 or justify == "all" :
1815
1816
return strings
0 commit comments