Skip to content

Commit 21d94b2

Browse files
committed
use CountConfigs namedtuple
1 parent ddf9efc commit 21d94b2

File tree

2 files changed

+64
-47
lines changed

2 files changed

+64
-47
lines changed

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4614,7 +4614,7 @@ def replace(
46144614
-------------- -----
46154615
1000000 non-null object
46164616
dtypes: object(1)
4617-
memory usage: 55.3 MB"""
4617+
memory usage: 62.9 MB"""
46184618
),
46194619
see_also_sub=(
46204620
"""

pandas/io/formats/info.py

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABCMeta, abstractmethod
22
import sys
3-
from typing import IO, TYPE_CHECKING, List, Optional, Tuple, Union, cast
3+
from typing import IO, TYPE_CHECKING, List, NamedTuple, Optional, Tuple, Union, cast
44

55
from pandas._config import get_option
66

@@ -15,6 +15,49 @@
1515
from pandas.core.series import Series # noqa: F401
1616

1717

18+
class CountConfigs(NamedTuple):
19+
"""
20+
Configs with which to display counts.
21+
22+
Attributes
23+
----------
24+
counts : Series
25+
Non-null count of Series (or of each column of DataFrame).
26+
count_header : str
27+
Header that will be printed out above non-null counts in output.
28+
space_count : int
29+
Number of spaces that count_header should occupy
30+
(including space before `dtypes` column).
31+
len_count : int
32+
Length of count header.
33+
count_temp : str
34+
String that can be formatted to include non-null count.
35+
"""
36+
37+
counts: "Series"
38+
count_header: str
39+
space_count: int
40+
len_count: int
41+
count_temp: str
42+
43+
44+
class HeaderAndSpaceConfigs(NamedTuple):
45+
"""
46+
Attributes
47+
----------
48+
space_dtype : int
49+
Number of spaces that `dtypes` column should occupy.
50+
header : str
51+
Header with extra columns (count and type) appended.
52+
len_dtype : int
53+
Length of dtype header.
54+
"""
55+
56+
space_dtype: int
57+
header: str
58+
len_dtype: int
59+
60+
1861
def _put_str(s: Union[str, Dtype], space: int) -> str:
1962
"""
2063
Make string of specified length, padding to the right if necessary.
@@ -74,7 +117,7 @@ def _sizeof_fmt(num: Union[int, float], size_qualifier: str) -> str:
74117

75118
def _get_count_configs(
76119
counts: "Series", col_space: int, show_counts: bool, col_count: Optional[int] = None
77-
) -> Tuple[str, int, int, str]:
120+
) -> CountConfigs:
78121
"""
79122
Get configs for displaying counts, depending on the value of `show_counts`.
80123
@@ -91,15 +134,7 @@ def _get_count_configs(
91134
92135
Returns
93136
-------
94-
count_header : str
95-
Header that will be printed out above non-null counts in output.
96-
space_count : int
97-
Number of spaces that count_header should occupy
98-
(including space before `dtypes` column).
99-
len_count : int
100-
Length of count header.
101-
count_temp : str
102-
String that can be formatted to include non-null count.
137+
CountConfigs
103138
"""
104139
if show_counts:
105140
if col_count is not None and col_count != len(counts): # pragma: no cover
@@ -117,17 +152,15 @@ def _get_count_configs(
117152
space_count = len(count_header)
118153
len_count = space_count
119154
count_temp = "{count}"
120-
return count_header, space_count, len_count, count_temp
155+
return CountConfigs(counts, count_header, space_count, len_count, count_temp)
121156

122157

123158
def _display_counts_and_dtypes(
124159
lines: List[str],
125160
ids: "Index",
126161
dtypes: "Series",
127162
show_counts: bool,
128-
counts: "Series",
129-
count_temp: str,
130-
space_count: int,
163+
count_configs: CountConfigs,
131164
space_dtype: int,
132165
space: int = 0,
133166
space_num: int = 0,
@@ -145,13 +178,8 @@ def _display_counts_and_dtypes(
145178
Series dtype (or dtypes of DataFrame columns).
146179
show_counts : bool
147180
Whether to show non-null counts.
148-
counts : Series
149-
Non-null counts of Series (or of each of DataFrame's columns).
150-
count_temp : str
151-
String that can be formatted to include non-null count.
152-
space_count : int
153-
Number of spaces that count_header should occupy
154-
(including space before `dtypes` column).
181+
count_configs: CountConfigs
182+
Configs with which to display counts.
155183
space_dtype : int
156184
Number of spaces that `dtypes` column should occupy.
157185
space : int = 0
@@ -168,12 +196,14 @@ def _display_counts_and_dtypes(
168196
line_no = _put_str(f" {i}", space_num)
169197
count = ""
170198
if show_counts:
171-
count = counts[i]
199+
count = count_configs.counts[i]
172200

173201
lines.append(
174202
line_no
175203
+ _put_str(col, space)
176-
+ _put_str(count_temp.format(count=count), space_count)
204+
+ _put_str(
205+
count_configs.count_temp.format(count=count), count_configs.space_count
206+
)
177207
+ _put_str(dtype, space_dtype)
178208
)
179209

@@ -439,30 +469,26 @@ def _verbose_repr(
439469

440470
header = _put_str(id_head, space_num) + _put_str(column_head, space)
441471
counts = self.data.count()
442-
count_header, space_count, len_count, count_temp = _get_count_configs(
443-
counts, col_space, show_counts, col_count
444-
)
472+
count_configs = _get_count_configs(counts, col_space, show_counts, col_count)
445473

446474
space_dtype, header, len_dtype = _get_header_and_spaces(
447-
dtypes, space_count, count_header, header
475+
dtypes, count_configs.space_count, count_configs.count_header, header
448476
)
449477

450478
lines.append(header)
451479
lines.append(
452480
_put_str("-" * len_id, space_num)
453481
+ _put_str("-" * len_column, space)
454-
+ _put_str("-" * len_count, space_count)
455-
+ _put_str("-" * len_dtype, space_dtype)
482+
+ _put_str("-" * count_configs.len_count, count_configs.space_count)
483+
+ _put_str("-" * len_dtype, space_dtype,)
456484
)
457485

458486
_display_counts_and_dtypes(
459487
lines,
460488
ids,
461489
dtypes,
462490
show_counts,
463-
counts,
464-
count_temp,
465-
space_count,
491+
count_configs,
466492
space_dtype,
467493
space,
468494
space_num,
@@ -489,29 +515,20 @@ def _verbose_repr(
489515
id_space = 2
490516

491517
counts = cast("Series", self.data._constructor(self.data.count()))
492-
count_header, space_count, len_count, count_temp = _get_count_configs(
493-
counts, id_space, show_counts
494-
)
518+
count_configs = _get_count_configs(counts, id_space, show_counts)
495519

496520
space_dtype, header, len_dtype = _get_header_and_spaces(
497-
dtypes, space_count, count_header
521+
dtypes, count_configs.space_count, count_configs.count_header
498522
)
499523

500524
lines.append(header)
501525
lines.append(
502-
_put_str("-" * len_count, space_count)
526+
_put_str("-" * count_configs.len_count, count_configs.space_count)
503527
+ _put_str("-" * len_dtype, space_dtype)
504528
)
505529

506530
_display_counts_and_dtypes(
507-
lines,
508-
ids,
509-
dtypes,
510-
show_counts,
511-
counts,
512-
count_temp,
513-
space_count,
514-
space_dtype,
531+
lines, ids, dtypes, show_counts, count_configs, space_dtype,
515532
)
516533

517534
def _non_verbose_repr(self, lines: List[str], ids: "Index") -> None:

0 commit comments

Comments
 (0)